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.
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);
}
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.