I tried the operator overload: link errors

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class GRectangle : public GObject{
public:
	
	GRectangle(int x, int y, int width, int height);
	~GRectangle(){
		if (handles.size()){
			vector<CRect*>::iterator it;
			for (it = handles.begin(); it != handles.end(); it++) delete *it;
		}
	}
	virtual void GRectangle::Draw(CDC* dc);
	virtual void LatchPt(CDC& dc,int x, int y);
	GObject* HandleHit(CPoint p);
	void AdjustToLatchPt();
	virtual void InitHandles();
	virtual CString IsClass(){ return CString("GRectangle"); }
private:
	
	int what;
	vector<CRect*> handles;
	int num_handles;

};

ostream& operator << (ostream& os, GObject& gob){
	long x = gob.GetBeginPt().x;
	os << x;
	return os;
}


Now I get link errors:


1>------ Build started: Project: mydraw, Configuration: Debug Win32 ------
1> GRectangle.cpp
1> Generating Code...
1> Compiling...
1> mydrawView.cpp
1> mydrawDoc.cpp
1> GObject.cpp
1> Generating Code...
1> Skipping... (no relevant changes detected)
1> mydraw.cpp
1>mydrawDoc.obj : error LNK2005: "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,class GObject &)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@AAVGObject@@@Z) already defined in GRectangle.obj
1>C:\Users\steve\documents\visual studio 2013\Projects\mydraw\Debug\mydraw.exe : fatal error LNK1169: one or more multiply defined symbols found
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
I solved my problem. Evidently the new C++ standard doesn't like inline operator overloads.
Are you sure it does NOT work with inline?

It works for me with inline and without inline.
Last edited on
Topic archived. No new replies allowed.