integral size mismatch in argument; conversion supplied

Hey everyone, im just working with derived classes and a virtual function to calculate the area for shapes of the derived class. Here is the code

#include <iostream>
#include <iomanip>
using namespace std;
using std::fixed;


class Shape{
    public:
      Shape(int,int);
      Shape ();
      virtual ~Shape() =0{};
      virtual void print()const =0;
        virtual double calcArea() =0;
    protected:
     int  centreX,centreY;
   };

////// Shape implementation ////////////////////


Shape::Shape(int x, int y)
{
   centreX = x;
   centreY = y;
}

Shape::Shape()
{  
   centreX=0;
   centreY=0;
}

////////////// Circle ///////////////////////////////////
class Circle:public Shape{

   public:
   
      Circle(int x,int y,int r);
      ~Circle ();
      void print()const;
      double calcArea();
  protected:
    int radius;
};


double Circle::calcArea()
{
      return 3.142 * radius * radius;
}
      

Circle::Circle (int x,int y, int r) : Shape(x,y)
 {
   radius = r;
 }

Circle::~Circle(void)
{}

void Circle::print(void)const
 {
   cout << "Circle with radius " << radius << " at " << centreX
<< "," << centreY << endl;
 }

 /////////////// rectangle //////////////////////////////
class Rectangle:public Shape{
   public:
      Rectangle( int, int, int, int);
      ~Rectangle();
      void print()const;
      double calcArea();
  protected:
      int height;
      int width;
  };

////////////// rectangle impelemtation ////////////////////
Rectangle::Rectangle(int x,int y,int h,int w)
                    :Shape(x,y)
{
   height=h;
   width = w;
}


Rectangle::~Rectangle()
{}

void Rectangle::print()const
{
   cout <<"Rectangle is at " << centreX <<','<< centreY
         << " Height:" << height << " width:" << width <<endl;
}

double  Rectangle::calcArea()
{
      return height * width;
}
int main()
{
//      cout << fixed << setprecision( 2 );

   Circle *circleArray[2];
   Rectangle *rectangleArray[2];

   circleArray[0] = &Circle(3, 3, 5);
   circleArray[1] = &Circle(4, 4, 6);

   rectangleArray[0] = &Rectangle(2,2,2,2);
   rectangleArray[1] = &Rectangle(3,3,3,3);

 
      for(int i = 0; i < 2; i++)
      cout <<  circleArray[i]->calcArea << endl;
      cout << rectangleArray[i]->calcArea << endl;

      return 0;
}


With the cout statements in int main i get the following warnings and all it outputs is 1's..


warning C4761: integral size mismatch in argument; conversion supplied

warning C4761: integral size mismatch in argument; conversion supplied

Can anyone help please?
DiscoDanceDaveAsked:
Who is Participating?
 
jkrConnect With a Mentor Commented:
Tricky error :o)

  circleArray[0] = &Circle(3, 3, 5);
  circleArray[1] = &Circle(4, 4, 6);

  rectangleArray[0] = &Rectangle(2,2,2,2);
  rectangleArray[1] = &Rectangle(3,3,3,3);

The above creates temporary objects, which is not what you want. With

  circleArray[0] = new Circle(3, 3, 5);
  circleArray[1] = new Circle(4, 4, 6);

  rectangleArray[0] = new Rectangle(2,2,2,2);
  rectangleArray[1] = new Rectangle(3,3,3,3);

it works. Don't forget to delete the objects, though.
0
 
jkrCommented:
Instead of

     for(int i = 0; i < 2; i++)
    cout <<  circleArray[i]->calcArea << endl;
    cout << rectangleArray[i]->calcArea << endl;

use

    for(int i = 0; i < 2; i++)
    {
    cout <<  circleArray[i]->calcArea() << endl;
    cout << rectangleArray[i]->calcArea() << endl;
   }

You want to call 'calcArea()', not print it's address.
0
 
jkrCommented:
Oh, and note that there need to be brackets around the statements you want to have in the loop if it is more than one.
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
DiscoDanceDaveAuthor Commented:
I've tried to run the program again with the above changes, but now my program generates a runtime error when executed :O
0
 
jkrCommented:
That's because of

    for(int i = 0; i < 2; i++)

You're accessing the array indices 0, 1 and 2 - however, your arrays only have 2 elements:

  Circle *circleArray[2];
  Rectangle *rectangleArray[2];

That should be fine with

    for(int i = 0; i < 1; i++)
    {
   cout <<  circleArray[i]->calcArea() << endl;
   cout << rectangleArray[i]->calcArea() << endl;
  }

0
 
DiscoDanceDaveAuthor Commented:
Changed, but error still persists
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.