d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Include The Same Header File In Few Classes
Add Reply New Topic New Poll
Member
Posts: 2,769
Joined: Dec 24 2009
Gold: 14.00
May 13 2013 11:43pm
I'm paying 100 fg to help me figure this problem out.

Is there a way I can use a header file in my main program AND in a header file used by my main program? For example:

Code
Inside Class 3.h:

#include Class1.h
#include Class2.h


Code
Inside Main Program:

#include Class1.h
#include Class2.h
#include Class3.h


However I'm getting a "Redefinition" error, I've read about forward declaration but I'm not sure how to apply it here.

Edit:

I did
Code
Inside Class 3.h:

class Class1.h;
class Class2.h;


But it's saying expected unqualified-id before '.' token. So I think I'm supposed to use :: somewhere, but I'm not sure where/how.
It's also saying the member variables (objects of the Class 1 and 2) inside this Class 3.h have incomplete types

This post was edited by Foxic on May 14 2013 12:09am
Member
Posts: 2,302
Joined: Aug 11 2004
Gold: 767.23
May 14 2013 12:59am
put preprocessor guards in the header

#ifndef HEADERNAME_H
#define HEADERNAME_H

...your code goes here... prototypes, classes, etc.

#endif // HEADERNAME_H


This is pretty standard...should be lots more info if you Google/Bing "header preprocessor guards" or something like that.

Cheers!
Member
Posts: 2,769
Joined: Dec 24 2009
Gold: 14.00
May 14 2013 01:24am
I already have that, I didn't mention it because codeblocks automatically puts it in.

I'm having a real wtf moment here... There's also "undefined reference to Class1::Class1" when I put in an EMPTY CONTRUCTOR in class 3... i.e: Class3::Class3() { } Why the hell would I get a reference error when I'm not even using the other classes in this constructor? I think this may be linked in some way
Member
Posts: 4,605
Joined: Sep 15 2011
Gold: 9,464.00
May 14 2013 03:25am
You really need to post more of the code that you have. It's pretty worthless to talk about the errors you're getting without showing us any code.
Member
Posts: 3,386
Joined: May 4 2013
Gold: 1,780.00
May 14 2013 04:10am
Code
#pragma once
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll