I have abstract class:
#include <string>
using namespace std;
class AString
{
public:
virtual ~AString() {}
virtual const string& GetName() const = 0;
virtual const string& GetValue() const = 0;
virtual const int GetSize() const = 0;
};
And derivative class. I try to overload binary + in this class:
class SymbString : public AString
{
private:
string name;
string val;
public:
SymbString() {}
SymbString(string _name) : name(_name) {}
SymbString(string _name, string _val) : name(_name), val(_val) {}
const string& GetName() const { return name; }
const string& GetValue() const { return val; }
const int GetSize() const { return val.size (); }
const string& SetValue(string value)
{
val = value;
return val;
}
SymbString operator + (const SymbString &str1, const SymbString &str2)
{
}
};
But see error: SymbString operator + (const SymbString &str1, const SymbString &str2) must take either zero or one argument
What’s wrong?
Thank you.
You need to put it into
SymbString‘s enclosing namespace. Not as a member.Or as a member but then only specifying the right parameter. But then
"Hello" + symbwon’t work anymore, because the left side is not aSymbString. So it’s advisable to write it as a non-member inSymbString‘s namespace.Note that to be able to say
symb + "Hello"or the other way around or even writingSymbString s = "Hello";, you also need to accept achar const*as a constructor parameter. Otherwise, achar const*won’t be implicitly convertible to aSymbStringbecause that will require to first converting tostd::stringand then converting from that toSymbString. Two such user defined conversions are not allowed in a single implicit conversion sequence.