Archived

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

p-iddy

My classes are only recognized in .cpp files!

Recommended Posts

I have a real bitch of a problem. First of all I'd like to say I know how to use includes and headers! anyways, I've declared a few classes in some .h files, and when I try to use an instance of the class inside another class (in another .h file) the classes are not recognized! however, when I try to use an instance of the class in any .cpp file, the variable works with no complaints. And I do have the proper includes with all of my files. Please, somebody help me, this may be a setting in VC++, not syntax related. Thanks Oh yeah, the classes are not recognized in any .h file, and they are recognized in every .cpp file, insane eh? Edited by - p-iddy on October 29, 2001 7:57:00 PM

Share this post


Link to post
Share on other sites
try using .hpp for headers and .cpp for source files. most compilers wont recognise anything but those for c++ specific stuff, like classes, etc

Share this post


Link to post
Share on other sites
Why are you declaring variables in a header file? They should be declared as extern in a header file and have the actual declaration in a cpp file. This avoids multiple declarations of the same variable.

Most C++ compilers recognize .c files as standard C source and .cpp files as C++ source. Same goes for .h and .hpp files.

Steve ''Sly'' Williams  Monkey Wrangler  Krome Studios

Share this post


Link to post
Share on other sites
Actually, many compilers don''t care about your header file extension. If you include your classes in the correct order (even in other header files), you should be able to correctly use them.

What do you mean when you say "use an instance of the class inside another class"? Do you mean one class has a member object (or pointer)? Post a relevant piece of code.

Share this post


Link to post
Share on other sites
is this the way you''ve declared your classes?

------------------
#include "b.h"
class a
{
b *m_pointer_to_b;
}
------------------
#include "a.h"
class b
{
a *m_pointer_to_a;
}
------------------

if so, you can change them to:

------------------
#include "b.h"
class b;
class a
{
b *m_pointer_to_b;
}
------------------
#include "a.h"
class a;
class b
{
a *m_pointer_to_a;
}
------------------


rapso

Share this post


Link to post
Share on other sites
Use forward declarations in your header files;

  
//HeaderA.h

class B;

class A
{
B instance;
};


Using forward declarations tells the compiler that the class B will be defined somewhere else. However, you can only use it''s name in the header. You can''t access its methods. You''re supposed to accesss them in your.cpp

Also, remember to include the HeaderB.h in your .cpp file.

Share this post


Link to post
Share on other sites
That will not work Darkor, you cannot instance B without having a definition for it. A could contain a pointer to a object of type B, but could not actually create an object of type B without having a definition for it.

--michael

Share this post


Link to post
Share on other sites