Solved

# integral size mismatch in argument; conversion supplied

Posted on 2005-05-04
Medium Priority
1,486 Views
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

0
Question by:DiscoDanceDave
• 4
• 2

LVL 86

Expert Comment

ID: 13929134

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

LVL 86

Expert Comment

ID: 13929144
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 Comment

ID: 13929180
I've tried to run the program again with the above changes, but now my program generates a runtime error when executed :O
0

LVL 86

Expert Comment

ID: 13929221
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 Comment

ID: 13929245
Changed, but error still persists
0

LVL 86

Accepted Solution

jkr earned 380 total points
ID: 13929312
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

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
###### Suggested Courses
Course of the Month14 days, 21 hours left to enroll