linker error for friend function

Hi ,
I have a class CPlant .. this is the header function Plant.h


1
2
3
4
5
6
7
8
9
10
11
12
13
14
class CPlant
{
public:
	int i;
public:
	CPlant(void);
	CPlant(int ii);
	~CPlant(void);
	friend const CPlant& operator+( const CPlant& a, const CPlant& b);
};
const CPlant& operator+( const CPlant& a, const CPlant& b)
{
	return CPlant(a.i + b.i);
}


This give me linker error as

1>Plant.obj : error LNK2005: "class CPlant const & __cdecl operator+(class CPlant const &,class CPlant const &)" (??H@YAABVCPlant@@ABV0@0@Z) already defined in TestFile1.obj



please let me know why this error ..and also how this error is remove.
Thanks in advance .

Just for the record you want to return by value not by reference. As soon as operator+ returns the temp object has been destroyed which invalidates the reference. As far as the linker error goes, do you have guards or did you define the function twice? It looks like a multiply defined symbol error.
Hi ,
Thanks for the quick replay .

do you have guards or did you define the function twice

I have not defined or declared the friend function twice.
please can you elaborate on what do you mean by [code ]do you have guards [/code].
Also when i use the return type as value it gives me same linker error.
I am not able to understand why ?
Try putting a prototype above the class definition.
No i have tried to put the function decleration above the class first .
also I have tried to put the function defination above the class .
But in both case i get numerious error( but not the linker errors.
First, you should prototype function operator+ before definition class CPlant. But that prototype uses CPlant that isn't known at the moment of prototyping. Thus, the follow double prototype should be added:

1
2
class CPlant;
const CPlant operator+ (const CPlant&, const CPlant&);


Second, as far as all objects inside function operator+ will be destroyed after execution, you cannot pass the result by reference.

Third, (default) assignment operator will be called at the end of operator+, which now looks like

1
2
3
4
5
6
const CPlant operator+(const CPlant& a, const CPlant& b)
{
  CPlant tmp;
  tmp.i = a.i + b.i;
  return (tmp);
}

Last edited on
serge , I tried with all the points that you have written .. but nothing works . same linker error.
Can't understand why this error is coming ?
please ...help me clear my doubt.
Thanks in advance .
It seems you're lacking include guards. You're probably including the same header file in Plant.cpp and in TestFile1.cpp. You should keep function definitions in .cpp files, too.

This is an include guard:
1
2
3
4
5
6
#ifndef PLANT_H
#define PLANT_H

// class definitions

#endif 

It prevents the same header file from being included twice during compilation.
I don't get the error here, but you could try making your operator+() a member:

1
2
3
4
5
6
7
8
9
10
11
12
13
class CPlant
{
public:
	int i;
public:
	CPlant(void);
	CPlant(int ii);
	~CPlant(void);
	CPlant operator+(const CPlant& rhs) const
	{
		return CPlant(i + rhs.i);
	}
};
Thanks you filipe ... it worked ...thanks all of you gurus ..
Topic archived. No new replies allowed.