Sign in to follow this  
Dasil

My Class Is Not Recognized

Recommended Posts

Hi again, I'm creating my own rectangle class, and I'm having a problem: I've created a header, Rectangle.h, which contains the class Rectangle. The implementation file works and recognizes Rectangle. When I try to use the class in another cpp file, I get errors:
error C2146: syntax error : missing ';' before identifier 'rectangle'
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error C2365: 'Rectangle' : redefinition; previous definition was 'function'
1>        c:\program files\microsoft sdks\windows\v6.0a\include\wingdi.h(3667) : see declaration of 'Rectangle'
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error C2228: left of '.create' must have class/struct/union
1>        type is 'int'
I've included Rectangle.h and I get these errors when I try to create a Rectangle object and use it.

Share this post


Link to post
Share on other sites
Wingdi.h has a Rectangle function:

__gdi_entry WINGDIAPI BOOL WINAPI Rectangle(__in HDC hdc, __in int left, __in int top, __in int right, __in int bottom);


So it looks like your program is getting this function confused with your class. Try changing the name, maybe something like CustomRectangle (or whatever).

Share this post


Link to post
Share on other sites
Thanks, that fixed it.

Is there a way that I can force the compiler to use my class (called Rectangle) instead of the Rectangle function? This way I wouldn't have to change the name, I don't mind changing it, I'm just curious.

Share this post


Link to post
Share on other sites
Yeah unfortunately windows puts all of it's stuff into the global namespace which often causes these kinds of problems (along with all of it's #defines...).

One solution is to put all of your own code into a namespace.
//myRectangle.h
namespace dasil {

class Rectangle
{
//...
};

}//end namespace dasil
//my cpp
#include "windows stuff"
#include "MyRectangle.h"

dasil::Rectangle r1;
//or
namespace dasil {
Rectangle r2;
}


Or you can screw around with windows like:
//my cpp
#define Rectangle DontUseTheName_Rectangle_PlzKThxBai
#include "windows stuff"
#undef Rectangle

#include "MyRectangle.h"

Rectangle r;

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this