header file not declared (REALLY SIMPLE)

I've done this before, but NOW it won't work

Page 1 of 1

9 Replies - 2287 Views - Last Post: 08 March 2009 - 09:24 AM Rate Topic: -----

#1 crazyjugglerdrummer  Icon User is offline

  • GAME OVER. NERD WINS.
  • member icon

Reputation: 119
  • View blog
  • Posts: 690
  • Joined: 07-January 09

header file not declared (REALLY SIMPLE)

Posted 06 March 2009 - 06:19 PM

I swear I have been programming too long to be having a simple issue like this (although I admit that I am human xD). I've done this before a few times, and it worked then.

here's a.h
#ifndef a_h
#define a_h

#include "b.h"

class a
{
public:
	void blahblah(b& thing);
};

#endif



here's b.h
#ifndef b_h
#define b_h

#include "a.h"

class b
	{
	public:
		
		void blah(a& thing);
		
	};
#endif



I made everything as simple as possible, there's no other code involved. But for some reason it tells me that a is not declared and b is not declared. This has got to be one of my typical really stupid mistakes, although I'm worried it could be a compiler setting (though I changed absolutely nothing). I've done this exact same type of thing before, but it just doesn't want to work today.

Thanks in advance!

Is This A Good Question/Topic? 0
  • +

Replies To: header file not declared (REALLY SIMPLE)

#2 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3122
  • View blog
  • Posts: 19,167
  • Joined: 14-September 07

Re: header file not declared (REALLY SIMPLE)

Posted 06 March 2009 - 07:00 PM

Weird. I cannot recreate it:

#ifndef HEADERTWO_H
#define HEADERTWO_H

#include "headerOne"

class HeaderTwo {

public:
	HeaderTwo() {};
	void function(HeaderOne& one)		{};
};
#endif



#ifndef HEADERONE_H
#define HEADERONE_H

#include "headerTwo.h"

class HeaderOne {

public:
	HeaderOne() {};
	void function(HeaderTwo& two)		{};
};
#endif



Is it a typical cannot find variable error?
Was This Post Helpful? 0
  • +
  • -

#3 crazyjugglerdrummer  Icon User is offline

  • GAME OVER. NERD WINS.
  • member icon

Reputation: 119
  • View blog
  • Posts: 690
  • Joined: 07-January 09

Re: header file not declared (REALLY SIMPLE)

Posted 07 March 2009 - 07:24 AM

Yes, it simply says that a has not been declared. The problem arises whenever I include one or both of them in another file. I admit that the only other code involved in the error was the #include "a.h" in the implementation file and likewise for the b implementation. It compiles fine without that, but I'd be nice if I could actually include the classes I make somewhere else. :D I can't think of what could cause this, as I've done it before, but now it just doesn't work...

Thanks for the reply!
Was This Post Helpful? 0
  • +
  • -

#4 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3122
  • View blog
  • Posts: 19,167
  • Joined: 14-September 07

Re: header file not declared (REALLY SIMPLE)

Posted 07 March 2009 - 07:31 AM

Did you include them both in another file which is giving problems? Can I see how you declared/included them?
Was This Post Helpful? 0
  • +
  • -

#5 crazyjugglerdrummer  Icon User is offline

  • GAME OVER. NERD WINS.
  • member icon

Reputation: 119
  • View blog
  • Posts: 690
  • Joined: 07-January 09

Re: header file not declared (REALLY SIMPLE)

Posted 07 March 2009 - 07:40 AM

Sure, but I'm not sure how enlightening it is XD. It has problems compiliting b.cpp, which contains only one line:
#include "b.h"


If I include both in the same file, it only gives the error for one of them (though I have seen it switch which one it complains about when I move all the code to a new project). I get one error whenever I include one or both of the files in a different file, griping about one of the classes being not declared in the header file. Could you try this in your compiler to see if I am going crazy? Thanks!
Was This Post Helpful? 0
  • +
  • -

#6 crazyjugglerdrummer  Icon User is offline

  • GAME OVER. NERD WINS.
  • member icon

Reputation: 119
  • View blog
  • Posts: 690
  • Joined: 07-January 09

Re: header file not declared (REALLY SIMPLE)

Posted 07 March 2009 - 04:43 PM

KYA, do you get problems when you try to include one of your or my header files? That's where my error arises, when I make an implementation file that includes the header. Please help! :@
Was This Post Helpful? 0
  • +
  • -

#7 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3122
  • View blog
  • Posts: 19,167
  • Joined: 14-September 07

Re: header file not declared (REALLY SIMPLE)

Posted 07 March 2009 - 05:07 PM

View Postcrazyjugglerdrummer, on 7 Mar, 2009 - 04:43 PM, said:

KYA, do you get problems when you try to include one of your or my header files? That's where my error arises, when I make an implementation file that includes the header. Please help! :@


I'm getting that too. If I include my headers in main or some cpp its telling me HeaderOne isn't defined. I can't think of the reason off the top of my head... Hopefully it will come to me in a few.


edit: Any chance you could rework the structure to avoid such circular dependencies? It's usually an early indicator of bad design.

This post has been edited by KYA: 07 March 2009 - 05:41 PM

Was This Post Helpful? 0
  • +
  • -

#8 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3122
  • View blog
  • Posts: 19,167
  • Joined: 14-September 07

Re: header file not declared (REALLY SIMPLE)

Posted 07 March 2009 - 05:45 PM

Sorry for the double post, but here is a possible solution. Have a third header that holds the class prototypes:

Main.h (names can be w/e, just an example)
#ifndef MAIN_H
#define MAIN_H
class HeaderOne;
class HeaderTwo;
#endif



headerOne.h
#ifndef HEADERONE_H
#define HEADERONE_H
#include "Main.h"
class HeaderOne {

public:
	HeaderOne() {};
	void function(HeaderTwo& two)		{};
};
#endif



headerTwo.h
#ifndef HEADERTWO_H
#define HEADERTWO_H
#include "Main.h"
class HeaderTwo {

public:
	HeaderTwo() {};
	void function(HeaderOne& one)		{};
};
#endif



Main.cpp:

#include "headerOne.h"
#include "headerTwo.h"

int main()
{ 
   HeaderOne one;
   HeaderTwo two;
   return 0;
}



Hope that helps.

This post has been edited by KYA: 07 March 2009 - 05:46 PM

Was This Post Helpful? 1
  • +
  • -

#9 crazyjugglerdrummer  Icon User is offline

  • GAME OVER. NERD WINS.
  • member icon

Reputation: 119
  • View blog
  • Posts: 690
  • Joined: 07-January 09

Re: header file not declared (REALLY SIMPLE)

Posted 08 March 2009 - 07:30 AM

Sweet! That worked! It's a little weird that you have a header that the other headers then implement, but I can see the logic of saying what the classes are, then implementing them later.

I assume since only the class names are mentioned in main.h, you would still need to have the #include "a.h" in the b file for the functions and variable names. My compiler doesn't care in this simple program, but if I actually had the function in a call the function in b I suppose it would matter as the compiler would need to know more about the class than just its name.

Thanks KYA! :D

This post has been edited by crazyjugglerdrummer: 08 March 2009 - 07:34 AM

Was This Post Helpful? 0
  • +
  • -

#10 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3122
  • View blog
  • Posts: 19,167
  • Joined: 14-September 07

Re: header file not declared (REALLY SIMPLE)

Posted 08 March 2009 - 09:24 AM

No, you'd be good to go. I added a headerOne.cpp as follows and it compiles clean:

code omitted

The linker will look for and find the class definitions as appropriate


There are undefined problems when doing that, unfortunately.

This post has been edited by KYA: 08 March 2009 - 01:37 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1