当我超负荷<<时,为什么会遇到错误在&#x2b返回的对象上;操作员重载功能
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题在于,重载
运算符的第二个参数不能绑定到
string
rvalue,因为第二个参数是 lvalue对非const的引用字符串
。您需要制作第二个参数以重载
运算符&lt;&lt;
aconst String&amp;
,以便它可以与第二个参数“ ab”
一起使用同样,如下所示:类似地在定义中执行相同的操作:
另外,请确保您的程序没有任何不确定的行为。例如,通过确保您仅在安全执行此操作时才使用
delete
或delete []
(不再需要指针点的数据)。您可以使用Valgrind等工具来检测一些基本问题。The problem is that the second parameter to overloaded
operator<<
cannot be bound to anString
rvalue since the second parameter is an lvalue reference to a non-constString
.You need to make the second parameter to overloaded
operator<<
aconst String&
so that it can work with the second argument"ab"
as well, as shown below:Similarly do the same in the definition:
Also, make sure that your program don't have any undefined behavior. For example, by making sure that you use
delete
ordelete[]
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.