My classes are only recognized in .cpp files!
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
try using .hpp for headers and .cpp for source files. most compilers wont recognise anything but those for c++ specific stuff, like classes, etc
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
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
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.
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.
MSVC takes the extension .C to mean "compile using C", and .CPP to mean "compile using C++".
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
------------------
#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
Use forward declarations in your header files;
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.
//HeaderA.hclass 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.
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
--michael
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement