Jump to content
  • Advertisement

Archived

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

eckbw

Including .h classfiles

This topic is 5655 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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



Share this post


Link to post
Share on other sites
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_H

class 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]

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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 error

public:
//...

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?

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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_H

class 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?

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!