My classes are only recognized in .cpp files!

Started by
7 comments, last by p-iddy 22 years, 5 months ago
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
Advertisement
try using .hpp for headers and .cpp for source files. most compilers wont recognise anything but those for c++ specific stuff, like classes, etc
Maybe you''re including stuff in the wrong order.
_______________________________
"To understand the horse you'll find that you're going to be working on yourself. The horse will give you the answers and he will question you to see if you are sure or not."
- Ray Hunt, in Think Harmony With Horses
ALU - SHRDLU - WORDNET - CYC - SWALE - AM - CD - J.M. - K.S. | CAA - BCHA - AQHA - APHA - R.H. - T.D. | 395 - SPS - GORDIE - SCMA - R.M. - G.R. - V.C. - C.F.
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
Steve 'Sly' Williams  Monkey Wrangler  Krome Studios
turbo game development with Borland compilers
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.
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
Use forward declarations in your header files;

  //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

This topic is closed to new replies.

Advertisement