重载二进制文件 +

发布于 2024-09-27 00:25:16 字数 1163 浏览 3 评论 0原文

我有抽象类:

#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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

胡渣熟男 2024-10-04 00:25:16

您需要将其放入 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 a SymbString. So it's advisable to write it as a non-member in SymbString's namespace.

Note that to be able to say symb + "Hello" or the other way around or even writing SymbString s = "Hello";, you also need to accept a char const* as a constructor parameter. Otherwise, a char const* won't be implicitly convertible to a SymbString because that will require to first converting to std::string and then converting from that to SymbString. Two such user defined conversions are not allowed in a single implicit conversion sequence.

感悟人生的甜 2024-10-04 00:25:16

您将其定义为实例方法。这意味着左操作数将是接收者 (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.

我为君王 2024-10-04 00:25:16

我猜您正在尝试引入 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 have operator+= as a member and implement free function operator+ through that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文