I just need some little explanation about this snippet, i did some little experiment in the class "value" and when i compiled the source file, error pops out, even though in my own logic, there is nothing wrong with it, i came with this book and im trying to learn copy constructors and operator overloading and i did some experimenting on the code:
This is the original code:
#include <iostream>
#include <sstream>
using namespace std;
class Value
{
public:
Value()
{
cout<<" Default constructor\n";
_numString=new char[1];
_numString[0]= '\0';
}
Value(int i)
{
cout<< "Construction/conversion from int "<<i<<endl;
stringstream buffer;
buffer<< i <<ends; //terminate sring
Init(buffer.str().c_str());
Display();
}
Value(Value const& v)
{
cout<< " Copy constructor("
<< v._numString<< ")\n";
Init(v._numString);
Display();
}
Value& operator= (Value const& v)
{
cout<<"operator=("
<<v._numString
<<")\n";
if(_numString != v._numString)
{
cout<<"not equal"<<endl;
delete _numString;
Init(v._numString);
}
Display();
return *this;
}
friend Value operator+ (Value const& v1,Value const& v2)
{
cout<<" operator+ ("<<v1._numString<< ", "
<<v2._numString <<")\n";
stringstream buffer;
buffer<<v1._numString<< " + "<<v2._numString<<ends;
Value result;
result.Init(buffer.str().c_str());
cout<<" Returning by value\n";
return result;
}
private:
void Init(char const* buf)
{
int len=strlen(buf);
_numString=new char[len + 1];
strcpy(_numString,buf);
}
void Display()
{
cout<< "\t" <<_numString<<endl;
}
char* _numString;
};
now what I did is that on line 50 i replaced it to
inline Value operator+ (Value const& v1,Value const& v2)
then the error is "must take zero to one argument"
and i also replaced line 50 to
Value& operator+ (Value const& v1,Value const& v2)
and still same error....
im wondering what's the explanation behind this, i just copied line 34... Thanks