Including .h classfiles

Started by
18 comments, last by eckbw 20 years, 10 months ago
I have a problem when trying to include .h files that contain both class declarations and classbody definitions. When I do just one #include "someclass.h" everything works fine. But when I want to use the class in some other module in the project, I get compiler errors like "multiple definitions of blablabla first defined here blabla etc..." The usual: #ifndef SOMECLASS_H #define SOMECLASS_H //class prototypes //class definitions //class body definitions #endif doesn´t seem to work. How can I keep both class declarations and class body definitions in a .h file and avoid getting those errors when I try to include it more than once?
Advertisement
the functions either need to be defined in the class:

//.h#ifndef DOG_H#define DOG_H#include <string>#include <iostream>class Dog {public:    Dog(const std::string name) : name_(name) {    }    Speak() {        std::cout << "Woof Woof Woof " << name_ << std::endl;    }private:    std::string name_;// edit: thanks kooktroop};#endif


edit:
or you could have them out of the body but only if you make them inline (as noted below).

[edited by - petewood on June 19, 2003 7:50:02 AM]
make sure you define
std::string name_; 


in the class too...

Also just as a matter of preference i use .hpp for C++ header files (those containing classes and/or namespaces etc...) you may want to as well.
Do not remove a fly from your friend's forehead with a hatchet.Chinese Proverb
quote:Original post by petewood
the functions need to be defined in the class


They don''t have to be. You can have them outside of the class declaration too.

//.h#ifndef DOG_H#define DOG_H#include <string>#include <iostream>class Dog{public:    inline Dog(const std::string name);    inline Speak();private:    std::string name_;};inline Dog::Dog(const std::string name)    : name_(name){}inline Dog::Speak(){    std::cout << "Woof Woof Woof " << name_ << std::endl;}#endif



quote:Original post by Poontardis

They don't have to be. You can have them outside of the class declaration too.


That´s what I´m trying to do, but I get "multiple definisions of blabla..." errors when I try to include the .h file more than once in the project. My .h files look like this:

#ifndef SOMECLASS_H#define SOMECLASS_Hclass SomeClass;class SomeClass{public://...private://...};SomeClass::SomeClass(){ }SomeClass::~SomeClass(){ }// etc...#endif // SOMECLASS_H


You´re saying this should work? Maybe it´s the compiler then? I´m using Dev-Cpp IDE and Mingw compiler.

[edited by - eckbw on June 19, 2003 7:20:48 AM]

[edited by - eckbw on June 19, 2003 7:21:19 AM]
If you allow yourself to have function bodies in the header file they shoud have a inline before the type specifier.So we will have this:

inline SomeClass::SomeClass(){}inline SomeClass::~SomeClass(){}



"You losers better learn...NOONE CONTROLS OUR GOD DAMN LIFE!!!" - MANOWAR
Ah ok, I think I get it now. Thanks for all the replys.
Arrgh, ok next problem.

I have two classes in two different .h files. One of them is an abstract baseclass, and I want the other class to be a subclass of this abstract baseclass. Like this:

#ifndef SOMECLASS_H#define SOMECLASS_H#include "someotherclass.h"class SomeClass;class SomeClass : public SomeOtherClass{ // here I get a syntax errorpublic://...private://...};// class bodies#endif


Now, problem is I get a "parse error before ''{'' token", like it wont recognize the ": public SomeOtherClass" part, even though I have included the other class. Am I doing something wrong here? Or is the problem in the "someotherclass.h" file? Or do I have to put the base and subclasses in the same .h file?
I tried putting a

class SomeOtherClass;

in there too, but then I get "class SomeOtherClass has incomplete type" error. I want to keep the classes in separate .h files but still be able to inherit from a baseclass, is that possible? Or do I have to put them all in a single .h file?
a) there''s no need to forward declare a class in its own header file (at least for what you''re coding just now anyway).

b) This will work fine:

//baseclass.h#ifndef BASECLASS_H#define BASECLASS_Hclass BaseClass{public:    // protected:    //private:    //};//inline class methods go here#endif //#ifndef BASECLASS_H////////////////////ChildClass.h#ifndef CHILDCLASS_H#define CHILDCLASS_H#include "BaseClass.h"class ChildClass : public BaseClass{public:    // protected:    //private:    //};//inline class methods go here#endif //#ifndef CHILDCLASS_H//////////////////


which is basically what you said you had. From the compiler error it looks like your error is actually in your someotherclass.h... do you have a semi-colon at the end of the someotherclass class declaration?

This topic is closed to new replies.

Advertisement