For my programming class, I have gotten to the lesson that involves overloading constructors and what not, but I have a problem with mine that I am possitive is a very simple syntax error or something.

If anyone could be so kind as to take a look at the following code.

#include <cstdlib>
#include <iostream>

using namespace std;

class rectangle
     {
     public:
          //constructors
          rectangle();
          rectangle(unsigned int width, unsigned int height);
          ~rectangle(){} //inline
          
          //overloaded draw function:
          void drawShape() const;
          void drawShape(unsigned int aWidth, unsigned int aHeight) const;
          
     private:
          unsigned int itsWidth;
          unsigned int itsHeight;
     };
     
     //1st overloaded constructor
     rectangle::rectangle()
          {
          itsWidth = 5;
          itsHeight = 12;
          }
     
     //second overloaded constructor
     rectangle::rectangle(unsigned int width, unsigned int height)
          {
          itsWidth = width;
          itsHeight = height;
          }
     
     //overloaded drawshape, which draws the rectangle based on the current value
     //takes no values
     void rectangle::drawShape() const
          {
          drawShape(itsWidth,itsHeight);
          }
     
     //overloaded drawshape, based on parameters given.
     //takes two values
     void rectangle::drawShape(unsigned int width, unsigned int height) const
          {
          for(unsigned int i = 0; i<height; i++)
               {
               for(unsigned int j = 0; j< width; j++)
                    {
                    //allows the drawing of superior rectangles
                    if(j != 0 && j != (width-1) && i != 0 && i != (height-1))
                         {
                         cout << " ";
                         }
                    else
                         {
                         cout << "*";
                         }
                    }
               cout << "\n";
               }
          }
          
int main()
     {
     unsigned int x;
     unsigned int y;
     
     cout << "Below is a rectangle drawn with the 1st overloaded constructor." << endl;
     
     rectangle theRect2();
     theRect2.drawShape();
     
     rectangle theRect(5,7);
     theRect.drawShape();
     cout << endl;
     
     cout << "Enter a value for the width of the new rectangle to be drawn: ";
     cin >> x;
     cout << "Enter a value for the height of the new rectangle to be drawn: ";
     cin >> y;
     
     theRect.drawShape(x,y);
     system("PAUSE");
     }

Recommended Answers

All 8 Replies

Member Avatar for rkamin1

I am possitive is a very simple syntax error or something.

What's the problem you're getting?

-Robbie

when I go to compile it, it says "request for member 'drawShape' in 'theRect2', which is of non-class type 'rectangle()()'"

It's a bit strang, but you never know the compilers...
Try: rectangle theRect2;
Instead of: rectangle theRect2();

Thanks guys, I knew it was something stupidly simple. Is it because that one needed no parameters, so I don't need to put in the parenthesis?

It is because the moment you put parameters, it becomes a function declaration rather than an object creation statement.

Hi i am new to cplus plus.
I tried this code , just removed the object name that you have taken is like a func call rectangle therect(); , but like a rectangle therect; works and just rerurning the main 0; return 0;
removing the #include<cstdlib> and system("pause");
(what is inclde<cstdlib> for i dont know abt this)
works nice

For my programming class, I have gotten to the lesson that involves overloading constructors and what not, but I have a problem with mine that I am possitive is a very simple syntax error or something.

If anyone could be so kind as to take a look at the following code.

#include <cstdlib>
#include <iostream>
 
using namespace std;
 
class rectangle
     {
     public:
          //constructors
          rectangle();
          rectangle(unsigned int width, unsigned int height);
          ~rectangle(){} //inline
 
          //overloaded draw function:
          void drawShape() const;
          void drawShape(unsigned int aWidth, unsigned int aHeight) const;
 
     private:
          unsigned int itsWidth;
          unsigned int itsHeight;
     };
 
     //1st overloaded constructor
     rectangle::rectangle()
          {
          itsWidth = 5;
          itsHeight = 12;
          }
 
     //second overloaded constructor
     rectangle::rectangle(unsigned int width, unsigned int height)
          {
          itsWidth = width;
          itsHeight = height;
          }
 
     //overloaded drawshape, which draws the rectangle based on the current value
     //takes no values
     void rectangle::drawShape() const
          {
          drawShape(itsWidth,itsHeight);
          }
 
     //overloaded drawshape, based on parameters given.
     //takes two values
     void rectangle::drawShape(unsigned int width, unsigned int height) const
          {
          for(unsigned int i = 0; i<height; i++)
               {
               for(unsigned int j = 0; j< width; j++)
                    {
                    //allows the drawing of superior rectangles
                    if(j != 0 && j != (width-1) && i != 0 && i != (height-1))
                         {
                         cout << " ";
                         }
                    else
                         {
                         cout << "*";
                         }
                    }
               cout << "\n";
               }
          }
 
int main()
     {
     unsigned int x;
     unsigned int y;
 
     cout << "Below is a rectangle drawn with the 1st overloaded constructor." << endl;
 
     rectangle theRect2();
     theRect2.drawShape();
 
     rectangle theRect(5,7);
     theRect.drawShape();
     cout << endl;
 
     cout << "Enter a value for the width of the new rectangle to be drawn: ";
     cin >> x;
     cout << "Enter a value for the height of the new rectangle to be drawn: ";
     cin >> y;
 
     theRect.drawShape(x,y);
     system("PAUSE");
     }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.