Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Base classes, .h and .cpp file question

Status
Not open for further replies.

pghsteelers

Technical User
Apr 21, 2006
121
US
I thought I had a grip on classes in reference to the header and executable code .cpp files. I was understanding that one held the declaration and one contained the actual code.

However, in getting into creating classes off of base classes, that I was running into errors such as:
error C2504: 'Class : base class undefined

The Help file states this error says it is declared by not defined. Attempts to see where I must not be referencing it using a #include directive, isn't taking care of the problem.

Therefore, I was hoping to find out if someone knew a simple way to explain who I should view the difference between a .h and .cpp file and what the correct way to append a new .cpp file for a new class (i.e. adding a class to an existing project) Should I create a .h file as well, or just a .cpp file and how to reference it corretly so I am not getting the C2504 that it isn't defined?
 
in general .h will contain your declaration of the class and the .cpp will contain the implimentation.

this however may not be the case if the class is very small and the implimentation is a few 1 liners or so (in which case it will sometimes all be in the .h).

if your class is inheriting from a base class or has members of some other class, add an incomplete class declaration to the .h file, and include the class header files in the .cpp file

for example

CSomeClass.h
Code:
#if !defined(SOMEBIGMANGLEDNAME)
#define SOMEBIGMANGLEDNAME

class CSomeOtherClass;

class CSomeClass
{
public:
    CSomeClass();
    virtual ~CSomeClass();
private
    CSomeOtherClass*    m_pSomeOtherClass;
};
#endif
CSomeClass.cpp
Code:
//implimentation of CSomeClass
#include "SomeOtherClass.h"
#include "SomeClass.h"

CSomeClass::CSomeClass()
{
    m_pSomeOtherClass = new CSomeOtherClass;
}

CSomeClass::~CSomeClass()
{
    delete m_pSomeOtherClass;
}

hope that makes sense :S

If somethings hard to do, its not worth doing - Homer Simpson
 
Thanks, I think the key was the whole #define statement that I am missing.
 
No matter how many, or what hierarchical type of classes you create, you always (except for explained small classes) create a .H file for declaration and .CPP file for implementation of EACH class. I.e.

Code:
// BaseClass.h
class CBaseClass
{
   // ...
};
Code:
// BaseClass.cpp
#include "BaseClass.h"
// ....
Code:
// DerivedClass.h
#include "BaseClass.h"

class CDerivedClass
{
   // ...
};
Code:
// DerivedClass.cpp
#include "DerivedClass.h"
// ....

Incomplete class declarations should only be used, when including a full declaration is not a case.

------------------
When you do it, do it right.
 
btw, missed the derivation

Code:
class CDerivedClass : CBaseClass
{
   // ...
};

------------------
When you do it, do it right.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top