重载二进制文件 +
我有抽象类:
#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;
};
和派生类。我尝试在此类中重载二进制 +:
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)
{
}
};
但看到错误:SymbString 运算符 + (const SymbString &str1, const SymbString &str2) 必须采用零个或一个参数
出了什么问题?
谢谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要将其放入
SymbString
的封闭命名空间中。不是以会员身份。或者作为成员但仅指定正确的参数。但是,
"Hello" + symb
将不再起作用,因为左侧不是SymbString
。因此建议将其写为SymbString
命名空间中的非成员。请注意,要能够说出
symb + "Hello"
或相反的方式,甚至编写SymbString s = "Hello";
,您还需要接受 char const* 作为构造函数参数。否则,char const*
不会隐式转换为SymbString
,因为这需要首先转换为std::string
,然后将其转换为SymbString
。在单个隐式转换序列中不允许有两个此类用户定义的转换。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" + symb
won'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 aSymbString
because that will require to first converting tostd::string
and then converting from that toSymbString
. Two such user defined conversions are not allowed in a single implicit conversion sequence.您将其定义为实例方法。这意味着左操作数将是接收者 (
this
),右操作数将是函数的参数。如果您希望两个操作数都是参数,则需要将该方法定义为静态方法,或定义为类外部的函数。
You're defining it as an instance method. This means that the left operand will be the receiver (
this
) and the right operand will be the argument to the function.If you want both operands to be arguments, you need to define the method as static, or as function outside of the class.
我猜您正在尝试引入
operator+
作为类的成员函数。那是错误的。将其定义为自由函数。或者将operator+=
作为成员并通过它实现自由函数operator+
。I'm guessing you are trying to introduce
operator+
as a member function of the class. That's wrong. Define it as a free function. Or haveoperator+=
as a member and implement free functionoperator+
through that.