mega_creamery
mega_creamery

Reputation: 677

c++ Visual Studio error which is solved by changing the class name

I was trying to compile simple class declaration using the guide form this page.

http://www.cplusplus.com/doc/tutorial/classes/

It was pasted straight from the website and would not allow me to compile. I am using visual studio 2010.

This is the error I received:

error C2146: syntax error : missing ';' before identifier 'rec'

Changing the name of class solved the problem, but I could not find anywhere that Rectangle is a word reserved for anything VS or C++ related.

How could I work that out next time?

class Rectangle
{
    public:
      int width;
      int height;
};

int main( )
{   
   Rectangle rec;
}

class Rector
{
   public:
    int width;
    int height;
};

int main( )
{   
   Rector rec;
}

Upvotes: 1

Views: 396

Answers (1)

melak47
melak47

Reputation: 4850

If you include Windows.h or Wingdi.h, your class name likely conflicts with the Rectangle function.

You have several options:

  1. Put your Rectangle class in a namespace
  2. Don't include Windows.h unless absolutely necessary
  3. Refer to it using class Rectangle everywhere to disambiguate between the function and the class (as a last resort)

Upvotes: 3

Related Questions