Skip to main content
gamedev.net
🔒 Locked

C++ odd scope? problem

Started by walkingcarcass 5 replies 800+ views
Original Post
walkingcarcass
walkingcarcass
Hi there. I have a file a.h which declares and defines the ostream<<X operator for a class X. b.h includes a.h and main.cpp includes b.h In main.cpp this is fine: X test(init); cout << X; In b.h this is not fine: X test(init); cout << X; (Both examples are themselves in global scope.) The full error is two screenfulls of stuff which starts with no match for 'operator<<' in 'std::operator<< [with _Traits = std::char_traits<char>](((std::basic_ostream<char, std::char_traits<char> >&)(+o)) and continues in a similarly unreadable way. As far as I can tell, it's as if ostream<<X was never defined. Since main.cpp is only aware of the operator because it includes b.h which includes a.h where it is defined, surely b.h should see it too? I had a similar looking pile of errors when I tried to seperate the declerations into .h files and definitions into .cpp files, according to the stylistic advice in my book (I'm coming back to c++ after a few years' hiatus). I couldn't suss it out then and gave up, I think it's much the same problem. Any insights? Thanks a lot.
spraff.net: don't laugh, I'm still just starting...
SiCrane
SiCrane
Do you have a circular include? As in does a.h include b.h and b.h include a.h?
walkingcarcass
walkingcarcass
Both main.cpp and b.h include a.h, a.h has a "#ifndef...#endif" to prevent multiple inclusions, but it's definitely not circular.
spraff.net: don't laugh, I'm still just starting...
izhbq412
izhbq412
Quote:
(Both examples are themselves in global scope.)
You mean you're trying to call a function from outside any function? If you are, you can't.

You should really put your definitions in .cpp files. Sooner or later you'll be using more than one, and if you include your header in more than one .cpp file you'll get linker errors.

PS: Posting some of your code might be helpful;
___The essence of balance is detachment. To embrace a cause, to grow fond or spiteful, is to lose one''s balance after which, no action can be trusted. Our burden is not for the dependent of spirit. - Mayar, Third Keeper
walkingcarcass
walkingcarcass
In GGA.cpp the line cout<<rbe; is fine. In element.h, ostream& operator<< (ostream& o, const Element& e) (at the very bottom) the equivalent test line won't compile.

main: GGA.cpp
#include <iostream.h>#include "boxmodel.h"#include "element.h"int main(int, char**){	cout << "GGA:\n";		BoxEdge be(12);	BoxEdge& rbe = be;	cout << rbe; // FINE <<<<<<<<<<<<<	return 0;}


boxmodel.h
#ifndef __BOXMODEL_H__#define __BOXMODEL_H__ 1#include <iostream.h>class RelNum {	/* Relative Number	 * can be stored as exact integer	 * or fraction of some parent integer.	 *	 * Whenever one representation is	 * updated, so is the other.	 *	 * Invariant: fraction=integer/scale	 * */	private:		float _f;		int _i;	public:		// read-only public members		// f==0 <=> i==0 => scale unknown		int scale() const {return (_f==0.0f)?0:int(_i/_f);}		int integer() const {return _i;}		float fraction() const {return _f;}		RelNum(){_f=_i=0;}				friend ostream& operator<< (ostream& o, const RelNum& rn);};class BoxEdge {	private:		RelNum _min, _max, _size;		bool maxAlign;	public:				friend ostream& operator<< (ostream& o, BoxEdge& be);};ostream& operator<< (ostream &o, BoxEdge& be){	const char* open  = be.maxAlign ? "("  : "(*";	const char* close = be.maxAlign ? "*)" : ")";	if(be.wasFrac)	return o << open << be._min.fraction()		 << ' ' << be._max.fraction()		 << close << be._size.fraction();	return o << open << be._min.integer()		 << ' ' << be._max.integer()		 << close << be._size.integer();<}#endif // def __BOXMODEL_H__


element.h
#ifndef __ELEMENT_H__#define __ELEMENT_H__ 1#include "boxmodel.h"typedef int ElementID;typedef int ElementEventData;class Element {	public:		friend ostream& operator<< (ostream& o, const Element& e);	private:			BoxEdge horizontal, vertical;		BoxEdge window_horizontal, window_vertical;};ostream& operator<< (ostream& o, const Element& e){	const BoxEdge rbe(101);	return o << "horz=" << rbe// above not fine <<<<<<<<<<<<<<<<<<<</* << e.horizontal << " vert=" << e.vertical		 << " w_horz=" << e.window_horizontal		 << " w_vert=" << e.window_vertical*/;}#endif // ndef __ELEMENT_H__
spraff.net: don't laugh, I'm still just starting...
Enigma
Enigma
You don't have an operator<< that takes a const BoxEdge, only one that takes a non-const BoxEdge. Also your includes should be of <iostream>, not <iostream.h>. The latter is provided only for backwards compatibility. You'll also need to add namespace qualification to your iostreams after you've fixed this. And please edit your post to use [source] tags.

Enigma

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.