Jump to content
  • Advertisement
Sign in to follow this  
PenguinGLX

Declaration Problem

This topic is 4261 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi Im new bee help me plzz main.cpp I declare
Quote:
class MyWindow { public: static char* szAppName; int WID,HI; int left,top; WNDCLASSEX wc; HWND hwnd; MyWindow(); int AssignMe(char *,WNDPROC,HINSTANCE); ~MyWindow(); }; MyWindow MainWin; (global Variable)
in Camera.h I wrote :
Quote:
... class MyWindow; .....
in Camera.cpp :
Quote:
include <...Camera.h> extern MyWindow MainWin; ..... ..... int MidX=MainWin.left+(MainWin.WID>>1); int MidY=MainWin.top+(MainWin.HI>>1); .....
When I compile the module Camera.cpp I said:
Quote:
Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland Camera.cpp: Error E2315 Camera.cpp 84: 'left' is not a member of 'MyWindow', because the type is not yet defined in function Camera::Update() Error E2315 Camera.cpp 84: 'WID' is not a member of 'MyWindow', because the type is not yet defined in function Camera::Update() Error E2315 Camera.cpp 85: 'top' is not a member of 'MyWindow', because the type is not yet defined in function Camera::Update() Error E2315 Camera.cpp 85: 'HI' is not a member of 'MyWindow', because the type is not yet defined in function Camera::Update() *** 4 errors in Compile *** >Exit code: 1
I tried to redeclare the whole class in Camera.h And i compiled Camera.cpp just fine. But because Main.cpp has: include <..... Camera.h> so when I compiled Main.cpp it said
Quote:
rror E2238 c:\bcc55\files\projects\win-menu5\Main.cpp 35: Multiple declaration for 'MyWindow' Error E2344 c:\bcc55\files\projects\win-menu5\camera.h 8: Earlier declaration of 'MyWindow'
How can I solve this problem THANKS

Share this post


Link to post
Share on other sites
Advertisement
You can't declare a class in main.cpp and then reference an instance of it in Camera.cpp. I would definitely declare the class in either its own h file or in Camera.h and then reference that h file in both main.cpp and camera.cpp, and not bother declaring it in Camera.cpp. You'll also need to make sure the class is only defined once if you include the h file in more than one place, otherwise you'll get that "class already defined" error. The most common way to this is called an "#include guard", and looks like this:



#ifndef MYCLASS_H_
#define MYCLASS_H_

class MyClass
{
...

};

#endif



Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!