# 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:
};

double Circle::calcArea()
{
}

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

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

###### Who is Participating?

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

Commented:

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

Commented:
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

Author 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

Commented:
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

Author 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.