0
#include <iostream>

using namespace std;

class Rectangle;


int main(){
    Rectangle myRoom(5,10);

    cout << myRoom.getHeight() << endl;
    cout << myRoom.getLength() << endl;

    system("pause");
    return 0;
}

class Rectangle{
    private: 
        int height;
        int length;

    public: 
        Rectangle(int aHeight, int aLength){
            height = aHeight;
            length = aLength;
        }

        int getHeight(){
            return height;
        }

        int getLength(){
            return length;
        }
};

Compiler is telling me that Rectangle, getHeight, and getLength are undefined. Why is it that my class Rectangle is not being prototype'd so that I can define it under the main method? Could someone tell me what I'm doing wrong? Thank you.

Sam D20
  • 2,535
  • 5
  • 25
  • 43
  • first, put the Rectangle class in a header file. second, never use system("pause") it's not a good habit/practice. As to where you are wrong, it's probably because you declare your class after you main(). put them to the top after the includes – Thang Do May 12 '13 at 00:41

3 Answers3

2

This:

class Rectangle;

is a forward declaration that basically tells the compiler nothing but that class Rectangle exists. And since the compiler doesn't know anything else about this class, Rectangle is an incomplete type.

At this line:

Rectangle myRoom(5,10);

you are trying to create an instance of class Rectangle although the class Rectangle hasn't been defined before. Naturally, the forward declaration does not suffice in this case. The type Rectangle is still incomplete.

Have a look at When can I use a forward declaration?, you'll find there a nice explanation of what you can and cannot do with incomplete type.

Community
  • 1
  • 1
LihO
  • 41,190
  • 11
  • 99
  • 167
1

You need to put the class definition prior to the call to the constructor. Ideally, you'd put it in a header file.

Please get rid of the system("pause").

David Schwartz
  • 179,497
  • 17
  • 214
  • 278
1

There's no such thing as "prototyping the class" in any formal meaning of the term.

Class types can be declared (or forward-declared, as this sort of declaration is sometimes referred to), which is what you did by your

class Rectangle;

declaration. A class type declared in this way can only be used in very limited way. You can declare pointers and references to Rectangle, for one example. But you are not allowed to access any innards of such class, since the compiler knows nothing about those innards from such a declaration. You are not allowed to define objects of such class.

Class types can be defined, which is what you did with your

class Rectangle {
  ...
};

declaration. Once the class is defined (but not before), you can use it in any way you wish.

In your code you are attempting to define an object and access members of a class that has been declared, but not yet defined. This will not work and there's no way around it. You have to swap the definition of main and the definition of Rectangle in your code.

If you wish, you can define your member functions after main (Rectangle::getHeight etc.), but that's pretty much all you can do after main. Everything else should go before main.

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765