Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

BlueChip

C++ -> Class -> newbie -> problem ^__^

This topic is 5679 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 =) I've built a class CmyClass:
    
#ifndef MyClass_H
#define MyClass_H

class MyClass
{
	bool	m_bool;
	.....	
        .....
	DWORD   m_dword;
public:	
	MyClass();	
	~MyClass() { Destroy(); }	
        void Destroy();
        ...other function ....
};

#endif
  
Then in file MyCLass.cpp I've write all method and function...
    
#include "MyClass.h"

MyClass::MyClass()
{	
	......
}

void MyClass::Destroy()
{	
         ......
}
  
Now... I want use a MyCLass object in another Class MyApplication In MyApplication.h I've write this:
  
class MyApplication
{
    BOOL  m_bXXX;  
    INT   m_iYYY;
    MyClass* m_pZZZZ
.....
.......
}
  
The problem is that if I try to compile, I get this error message: 'MyClass': missing storage-class or type specifiers Why? Why doesn't see my class?? PS: in MyApplication.cpp I've write #include "MyClass.h" Good bye. [edited by - BlueChip on February 1, 2003 2:36:57 PM]

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
You do have myclass.cpp in your new project, right?

Share this post


Link to post
Share on other sites
MyApplication uses a pointer to MyClass so needs to know the name MyClass. There are a few ways to do this.

1) You can let it know by putting the header file for MyClass before the header for MyApplication. This can be a) in the cpp file or b) actually in the MyApplication.h file.

    
//a)in MyApplication.cpp

#include "MyClass.h"

#include "MyApplication.h"

//or

//b)in MyApplication.h


#include "MyClass.h"

class MyApplication
{
bool m_bXXX;
int m_iYYY;
MyClass* m_pZZZZ;


2) put a forward declaration, This is just the name of the class which satisfies the compiler until later. eg in MyApplication.h
Later when you are actually using the pointer to MyClass somewhere in the .cpp file you need to let the compiler see the whole declaration, that's so it knows what functions can be called etc. So in MyApplication.cpp you need to include MyClass.h


        
//in MyApplication.h

MyClass;// this satisfies the compiler

class MyApplication
{
bool m_bXXX;
int m_iYYY;
MyClass* m_pZZZZ;

//in MyApplication.cpp

#include "MyClass.h"

#include "MyApplication.h"


Out of the two methods the second is foolproof (no chance of circular dependencies) and decreases needless dependencies between seperate sections of code. It will reduce the need to recompile when a header is changed.

By the way, when posting code here you can format it using [ source ] [/ source ] tags (omitting the spaces)

[edited by - petewood on February 1, 2003 1:04:45 PM]

Share this post


Link to post
Share on other sites
YEP =)

Thanks very very much....
I don''t belive that order of header was important.

quote:
By the way, when posting code here you can format it using [ source ] [/ source ] tags (omitting the spaces)

Yes... I do it now...
perhaps this topic will be usefull to other people.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!