C++: Multiple definition?

Maui

Newbie
Joined
Jul 6, 2004
Messages
390
Reaction score
0
Hey guys. I'm working on a program right now that's dealing with polynomials, and my idea was to have a linked list of "term" objects that each held a coefficient and exponent int. But, of course, C++ hates me and wishes to frustrate even these simple efforts.

Code:
class Term
{
	public:
		void setCoefficient(int x) { coefficient = x; };
		void setExponent(int x) { exponent = x; } ;
		int getCoefficient( ) { return coefficient; };
		int getExponent( ) { return exponent; };
		Term( ) { coefficient = 0; exponent = 0; };
		Term(int x, int y) { coefficient = x; exponent = y; };
		Term& operator=(Term& rtSide);
	private:
		int coefficient;
		int exponent;
};

Term& Term::operator=(Term& rtSide)
{
	coefficient = rtSide.coefficient;
	exponent = rtSide.exponent;
	return *this;
}

returns this error message:
File: [dir]\TermNode.o(.text+0x0)
Message: In function 'ZN4TermaSERS_':
File: [dir]\Term.cpp
Message: [Line 17] multiple definition of 'Term::operator=[Term&]'
File: [dir]\Term.o(.text+0x0):[dir]\Term.cpp
Message: [Line 17] first defined here
File: [same]
Message: [Line 17] Id returned 1 exit status
File: [dir]\Makefile.win
Message: [Build Error] [CS256Proj3.exe] Error 1

It's clearly a problem with the makefile somewhere. For reference, I'm using Dev-C++ 4.9.9.2, which uses the latest MingW32 compiler I believe.

Any ideas on what's causing this?
 
Okay, this doesn't make any sense to me - I just changed the function prototype for the operator() method from 'Term& rtSide' to 'const Term& rtSide', and it now seems to work perfectly.

Why is that necessary? Making the right side a const value seems more in the realm of "good idea" than a necessary step.
 
That's the wierd thing though, this was in it's own source file - Term.cpp, in fact.
 
have you used the

#ifndef
#define

pre-processors in your header file? To prevent multiple inclusions the same class? Is your main function in a different file from the operator definition?
 
This is going to sound really stupid/n00bish - because it is - but I just discovered the magic of header files...up to this point I had just been including the actual *.cpp file in my main function. As soon as I moved to using headers (and #ifndef, #define, and #endif incidentally mortiz, thanks) the problem was completely solved. Thanks guys! :bounce:
 
Back
Top