当我超负荷<<时,为什么会遇到错误在&#x2b返回的对象上;操作员重载功能

发布于 2025-01-28 00:38:53 字数 1223 浏览 1 评论 0原文

class String
{
    char* array;
public:
    String(const char* s)
    {
        array = new char[strlen(s) + 1]{ '\0' };
        strcpy(array, s);
    }
    ~String()
    {
        if (array)
        {
            delete[]array;
        }
    }
    String operator+ (const char* p)   //返回对象
    {
        String temp(p);
        char* tempStr = temp.array;
        temp.array = new char[strlen(array) + strlen(tempStr) + 1]{ '\0' };
        strcpy(temp.array, array);
        strcat(temp.array, p);
        delete[]tempStr;
        return temp;
    }
    friend ostream& operator<<(ostream& output, String& x);   // <<函数重载只能定义成友元
};

ostream& operator << (ostream& output, String& x)  //对<<重载的方式
{
    output << x.array;
    return output;
}

int main()
{
    String string1("mystring");
    cout << string1 + "ab" << endl;
    cout << string1 << endl;
    return 0;
}

这是我第一次在这里问一个问题,所以请原谅我是否有任何不好的描述:)

回到这一点,我已经超载了++&lt;&lt;&lt; 运算符,所以我想通过cout&lt;&lt;&lt;&lt; string1+“ ab”&lt;&lt; endl,但输出被插入。

我认为+运算符函数可能会有问题,有人可以告诉我问题在哪里,

如果我想获得正确的结果,我应该如何重写过载的功能?

class String
{
    char* array;
public:
    String(const char* s)
    {
        array = new char[strlen(s) + 1]{ '\0' };
        strcpy(array, s);
    }
    ~String()
    {
        if (array)
        {
            delete[]array;
        }
    }
    String operator+ (const char* p)   //返回对象
    {
        String temp(p);
        char* tempStr = temp.array;
        temp.array = new char[strlen(array) + strlen(tempStr) + 1]{ '\0' };
        strcpy(temp.array, array);
        strcat(temp.array, p);
        delete[]tempStr;
        return temp;
    }
    friend ostream& operator<<(ostream& output, String& x);   // <<函数重载只能定义成友元
};

ostream& operator << (ostream& output, String& x)  //对<<重载的方式
{
    output << x.array;
    return output;
}

int main()
{
    String string1("mystring");
    cout << string1 + "ab" << endl;
    cout << string1 << endl;
    return 0;
}

This is my first time asking a question here, so please forgive me if there are any bad descriptions :)

Back to the point ,I have overloaded + and << operators,so I want to get the output "mystringab" by cout<<string1+"ab"<<endl,but the output is garbled.

I think there may be a problem with the + operator overloaded function,can someone please tell me where is the problem?

And if I want to get the correct result, how should I rewrite the overloaded function?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

遥远的绿洲 2025-02-04 00:38:53

问题在于,重载运算符的第二个参数不能绑定到string rvalue,因为第二个参数是 lvalue对非const的引用字符串

我应该如何重写超载函数?

您需要制作第二个参数以重载运算符&lt;&lt; a const String&amp;,以便它可以与第二个参数“ ab”一起使用同样,如下所示:

//---------------------------------------- vvvvv------------>low-level const added here
friend ostream& operator<<(ostream& output,const String& x);

类似地在定义中执行相同的操作:

//----------------------------------- vvvvv------------>low-level const added here
ostream& operator << (ostream& output,const String& x) 
{
    output << x.array;
    return output;
}

另外,请确保您的程序没有任何不确定的行为。例如,通过确保您仅在安全执行此操作时才使用deletedelete [](不再需要指针点的数据)。您可以使用Valgrind等工具来检测一些基本问题。

The problem is that the second parameter to overloaded operator<< cannot be bound to an String rvalue since the second parameter is an lvalue reference to a non-const String.

how should I rewrite the overloaded function?

You need to make the second parameter to overloaded operator<< a const String& so that it can work with the second argument "ab" as well, as shown below:

//---------------------------------------- vvvvv------------>low-level const added here
friend ostream& operator<<(ostream& output,const String& x);

Similarly do the same in the definition:

//----------------------------------- vvvvv------------>low-level const added here
ostream& operator << (ostream& output,const String& x) 
{
    output << x.array;
    return output;
}

Also, make sure that your program don't have any undefined behavior. For example, by making sure that you use delete or delete[] only when it is safe to do so(that data to which the pointer points is no longer needed). You can use tools such as valgrind to detect some basic problems.

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