?
Solved

integral size mismatch in argument; conversion supplied

Posted on 2005-05-04
6
Medium Priority
?
1,486 Views
Last Modified: 2008-02-01
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?
0
Comment
Question by:DiscoDanceDave
  • 4
  • 2
6 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 13929134
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
 
LVL 86

Expert Comment

by:jkr
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

by:DiscoDanceDave
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Expert Comment

by:jkr
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

by:DiscoDanceDave
ID: 13929245
Changed, but error still persists
0
 
LVL 86

Accepted Solution

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question