C++运算符重载 虚函数函数报错

关于C++运算符&&重载问题,程序出现报错_百度知道
关于C++运算符&&重载问题,程序出现报错
#include&iostream&
class Complex
Complex(){r=0;i=0;}
Complex(int a,int b):r(a),i(b){}
Complex operator +(Complex &obj)
return Complex(r+obj.r,i+obj.i);
friend ostream &operator && (os...
我有更好的答案
jpg" esrc="http.hiphotos.baidu.com/zhidao/wh%3D600%2C800/sign=a45119ca0afa513d51ff64d80d5d79c3/daaf91d1f2ddf9edab://f.hiphotos.baidu.com/zhidao/wh%3D450%2C600/sign=6c30844b57fbb2fb347ec92/daaf91d1f2ddf9edab.baidu、<img class="ikqb_img" src="http://f
为您推荐:
其他类似问题
运算符的相关知识
&#xe675;换一换
回答问题,赢新手礼包&#xe6b9;
个人、企业类
违法有害信息,请在下方选择后提交
色情、暴力
我们会通过消息、邮箱等方式尽快将举报结果通知您。C++种运算符重载后被析构时出错 - C++当前位置:& &&&C++种运算符重载后被析构时出错C++种运算符重载后被析构时出错www.MyException.Cn&&网友分享于:&&浏览:3次C++类运算符重载后被析构时出错!环境:VC++
问题:实现类运算符+重载,结果在析构时出现错误
FS(char*&s);
~FS(void);
FS&operator+(FS&f1);
operator&char*();
FS::FS(char*&s)
m_len&=&strlen(s);
m_buf&=&new&char[m_len&+&1];
memcpy(m_buf,&s,&m_len);
m_buf[m_len]&=&'\0';
FS::~FS(void)
SAFE_DELETE(m_buf);&&&//第3次析构时出错!此时m_buf="333"
m_len&=&0;
FS&FS::operator&+(FS&m)
char*&ss&=&new&char[this-&m_len&+&m.m_len&+&1];
memcpy(ss,&this-&m_buf,&this-&m_len);
memcpy(ss&+&this-&m_len,&m.m_buf,&m_len);
ss[this-&m_len&+&m.m_len]&=&'\0';
memcpy(this-&m_buf,&ss,&this-&m_len&+&m.m_len&+&1);
this-&m_len&=&this-&m_len&+&m.m_
FS::operator&char*()
调用:char*&sss&=&FS("11111")&+&"22222"&+&"33333";
在第3次调用析构时出现错误,提示“堆被损坏”。
请问这个该怎么修改?
------解决方案--------------------FS&FS::operator&+(FS&m)
&&&&char*&ss&=&new&char[this-&m_len&+&m.m_len&+&1];
&&&&memcpy(ss,&this-&m_buf,&this-&m_len);
&&&&memcpy(ss&+&this-&m_len,&m.m_buf,&m_len);
&&&&ss[this-&m_len&+&m.m_len]&=&'\0';
&&&&memcpy(this-&m_buf,&ss,&this-&m_len&+&m.m_len&+&1);
&&&&this-&m_len&=&this-&m_len&+&m.m_
&&&&return&*
返回类型改为引用类型FS&------解决方案--------------------请添加拷贝构造函数和赋值运算符
所有涉及到内存分配的类都应该自定义构造函数,拷贝构造函数,赋值运算和析构函数
你的代码在每次进行值传递时都会拷贝一次指针,导致指针被多析构一次------解决方案--------------------引用:Quote: 引用:
请添加拷贝构造函数和赋值运算符
所有涉及到内存分配的类都应该自定义构造函数,拷贝构造函数,赋值运算和析构函数
你的代码在每次进行值传递时都会拷贝一次指针,导致指针被多析构一次
按照这样修改了之后,可以使用FS&ss&=&FS("11111")&+"22222"&+&"33333";了,但是使用char*&ss&=&FS("11111")&+"22222"&+&"33333";时得到的字符串是错误的字符串("恰恰恰恰恰恰恰恰.."),调试发现是在给ss赋值前调用了析构函数释放了m_buf。请问这样我该怎么修改??
那是临对象在表达式终止处自动析构了,换成下面这样试试。
FS&ss&=&FS("11111")&+"22222"&+&"33333";
char*&ss&=&FS;
------解决方案--------------------memcpy(this-&m_buf,&ss,&this-&m_len&+&m.m_len&+&1);
这也能cpp啊------解决方案--------------------你这里好多错误啊,
&&&&char*&ss&=&new&char[this-&m_len&+&m.m_len&+&1];
&&&&memcpy(ss,&this-&m_buf,&this-&m_len);
&&&&memcpy(ss&+&this-&m_len,&m.m_buf,&m_len);
&&&&ss[this-&m_len&+&m.m_len]&=&'\0';
&&&&memcpy(this-&m_buf,&ss,&this-&m_len&+&m.m_len&+&1);
&&&&---&没有重新申请内存,能保证m_buf的内存足够用么?
&&&&this-&m_len&=&this-&m_len&+&m.m_
&&&&return&*
&&&&---&ss不是内存泄露么?
12345678910
12345678910
12345678910 上一篇:下一篇:文章评论相关解决方案 12345678910 Copyright & &&版权所有欢迎加入我们,一同切磋技术。 &
用户名: &&&
密 码: &
共有 1226 人关注过本帖
标题:关于C++语言运算符重载的问题!编译报错说存在运算符二义性,请问如何修改? ...
来 自:重庆邮电大学
等 级:论坛游民
帖 子:173
专家分:30
结帖率:88.24%
&&已结贴√
&&问题点数:20&&回复次数:7&&&
关于C++语言运算符重载的问题!编译报错说存在运算符二义性,请问如何修改?
#include&iostream&
#include&string&
class String
&&& String();
&&& String(char*p);
&&& void display();
&&& friend bool operator&(String &a,String &b);
&&& friend bool operator==(String &a,String &b);
&&& friend bool operator&(String &a,String &b);
&&& char *p;
bool operator&(String &a,String &b)
&&& if(strcmp(a.p,b.p)&0)
bool operator&(String &a,String &b)
&&& if(strcmp(a.p,b.p)&0)
bool operator==(String &a,String &b)
&&& if(strcmp(a.p,b.p)==0)
void String::display()
&&& cout&&p&&
String::String()
&&& p=NULL;
String::String(char*x)
int main()
&&& String a=String(&Hello&);
&&& String b=String(&Kimi&);
&&& cout&&(a&b)&&
&&& cout&&(a==b)&&
&&& cout&&(a&b)&&
搜索更多相关主题的帖子:
&&&&&&&&&&
来 自:重庆邮电大学
等 级:论坛游民
帖 子:173
专家分:30
编译信息如下:
C:\Users\123\Desktop\practice 5\Cpp1.cpp(61) : error C2593: 'operator &' is ambiguous
C:\Users\123\Desktop\practice 5\Cpp1.cpp(62) : error C2593: 'operator ==' is ambiguous
C:\Users\123\Desktop\practice 5\Cpp1.cpp(63) : error C2593: 'operator &' is ambiguous
C:\Users\123\Desktop\practice 5\Cpp1.cpp(64) : warning C4508: 'main' : function s 'void' return type assumed
执行 cl.exe 时出错.
Cpp1.exe - 1 error(s), 0 warning(s)
C语言好难啊~
来 自:流年
等 级:贵宾
威 望:82
帖 子:2786
专家分:14619
最后一句是主函数没有返回
一片落叶掉进了回忆的流年。
来 自:流年
等 级:贵宾
威 望:82
帖 子:2786
专家分:14619
忘了友元函数需不需要写String::不过你调用方式好像不对
一片落叶掉进了回忆的流年。
等 级:蜘蛛侠
帖 子:331
专家分:1433
&&得分:20&
简单说说:
VC6.0 对友员函数支持不够好~!
#include&iostream&
#include&string&
using std::&&
using std::&&
using std::&&
class String
&&& String();
&&& String(char*p);
&&& void display();
&&& friend bool operator&(String &a,String &b);
&&& friend bool operator==(String &a,String &b);
&&& friend bool operator&(String &a,String &b);
protected:
&&& char *p;
bool operator&(String &a,String &b)
&&if(strcmp(a.p,b.p)&0)
bool operator&(String &a,String &b)
&&& if(strcmp(a.p,b.p)&0)
bool operator==(String &a,String &b)
&&& if(strcmp(a.p,b.p)==0)
void String::display()
&&& cout&&p&&
String::String()
&&& p=NULL;
String::String(char*x)
int main()
&&& String a=String(&Hello&);
&&& String b=String(&Kimi&);
&&& cout&&(a&b)&&
&&& cout&&(a==b)&&
&&& cout&&(a&b)&&
&&& return 0;
来 自:重庆邮电大学
等 级:论坛游民
帖 子:173
专家分:30
#include&iostream&
class Complex
&&& Complex();
&&& Complex(int,int);
&&& ~Complex();
&&& Complex operator+(Complex&);
&&& friend ostream&operator&&(ostream&,Complex&);
ostream&operator&&(ostream&output,Complex&exp)
&&& output&&exp.real&&&+&&&exp.imag&&&i&&&
Complex Complex::operator+(Complex& a)
&&& c.real=real+a.
&&& c.imag=imag+a.
Complex::Complex()
&&& real=0;
&&& imag=0;
Complex::~Complex()
Complex::Complex(int a,int b)
&&& real=a;
&&& imag=b;
int main()
&&& Complex a=Complex(3,4);
&&& Complex b=Complex(4,-10);
&&& cout&&a&&
&&& cout&&b&&
//又是类似问题! 我明天要换成VC2010啦!
C语言好难啊~
来 自:重庆邮电大学
等 级:论坛游民
帖 子:173
专家分:30
换成VS2010相关问题迎刃而解!
C语言好难啊~
等 级:新手上路
#include&iostream&
#include&string&
class String
&&& String() { p = NULL; };
&&& String(char *x) { p = };
&&& void display();
&&& friend bool operator&(String &a, String &b);
&&& friend bool operator==(String &a, String &b);
&&& friend bool operator&(String &a, String &b);
&&& char *p;
bool operator&(String &a, String &b)
&&& if (strcmp(a.p, b.p)&0)
bool operator&(String &a, String &b)
&&& if (strcmp(a.p, b.p)&0)
bool operator==(String &a, String &b)
&&& if (strcmp(a.p, b.p) == 0)
void String::display()
&&& cout && p &&
int main()
&&& String a(&Hello&);
&&& String b(&Kimi&);
&&& cout && (a&b) &&
&&& cout && (a == b) &&
&&& cout && (a&b) &&
&&& system(&pause&);
&&& return 0;
我这样子改了一下 就可以了
版权所有,并保留所有权利。
Powered by , Processed in 0.200848 second(s), 7 queries.
Copyright&, BCCN.NET, All Rights Reserved82被浏览11,710分享邀请回答class TestClass {
int number;
TestClass& operator+=(const TestClass& rhs) {
number += rhs.number;
return *this;
上例是一个C++的不完整的简单类,其中+=运算符,它本身的意义是「自增,并返回自增后的值」,所以就要返回自己,而不是返回一个自己的拷贝。2. 在此情况下,返回值为NULL或类似情况怎么办?
引用不可为空,楼主所说的情况不存在。3. 返回的引用赋给一个变量后,那个变量是不是引用?怎么理解?#include &iostream&
using namespace std;
class StupidClass {
StupidClass(int flag): flag(flag) {
cout && "Constructor " && flag && endl;
~StupidClass() {
cout && "Destructor " && flag && endl;
StupidClass(const StupidClass& rhs) {
cout && "Copy Constructor *this=" && flag && " rhs=" && rhs.flag && endl;
StupidClass& operator=(const StupidClass& rhs) {
cout && "Operator = *this=" && flag && " rhs=" && rhs.flag && endl;
return *this;
StupidClass& operator+=(const StupidClass& rhs) {
cout && "Operator += *this=" && flag && " rhs=" && rhs.flag && endl;
flag += rhs.flag;
return *this;
int main() {
StupidClass var1(1), var2(2);
StupidClass var3 = var1 += var2;
输出是Constructor 1
Constructor 2
Operator += *this=1 rhs=2
Copy Constructor *this=3 rhs=3
Destructor 3
Destructor 2
Destructor 3
所以结果就是先执行var1 += var2,然后使用得来的var1对var3拷贝构造。其中var1中的flag变成3了(看后面两个Destructor 3,和Copy Constructor *this=3 rhs=3)。如果把var3的类型改成StupidClass&,那么输出就变成Constructor 1
Constructor 2
Operator += *this=1 rhs=2
Destructor 2
Destructor 1
那么var3就是var1的引用了。4. 据说返回局部对象的引用非常危险,,难道输入输出流返回的不是局部对象的引用?
输入输出流重载的是 && 和 && 运算符,一般是这样写的ostream& operator&&(ostream& os, const TestClass& rhs) {
os && rhs.number;
return os;
这里面没有返回局部对象的引用啊,所谓返回局部对象的引用是像这样的int& func() {
int dummy = 1;
return dummy;
在这里dummy就是一个「局部对象」。4311 条评论分享收藏感谢收起 class Widght{
Widget& operator=(cosnt Widget& rhs)
Widget& operator+=(cosnt Widget& rhs)
Widget& operator-=(cosnt Widget& rhs)
Widget& operator*=(cosnt Widget& rhs)
Widget& operator/=(cosnt Widget& rhs)
注意,这只是个协议,并无强制性,如果不遵循它,代码一样可以通过编译,然而这份协议被所有内置类型和标准程序库提供的类型入string,vector,complex,trl:shared_ptr或者即将提供的类型共同遵守。因此除非你有一个标新立异的好理由,不然还是随众吧。下面看一个赋值运算符号的例子#ifndef STRING_H
#define STRING_H
class String
String(const char* s);
String operator=(const String& another);
void show()
cout&&"value = "&&str&&
/*copy construct*/
String(const String& other)
len = other.
str = new char[len + 1];
strcpy(str,other.str);
cout&&"copy construct"&&
cout&&"deconstruct"&&
#endif // STRING_H
#include&iostream&
#include&string.h&
#include"String.h"
String::String(const char* s)
len = strlen(s);
str = new char[len + 1];
strcpy(str,s);
String String::operator=(const String &other)
if(this == &other)
len = other.
str = new char[len + 1];
strcpy(str,other.str);
int main()
String str1("abc");
String str2("123");
String str3("456");
str1.show();
str2.show();
str3.show();
str3 = str1 = str2;//str3.operator=(str1.operator=str2)
str3.show();
str1.show();
下面是返回引用的情况String& operator+(const String& str) 这样会在赋值运算过程的返回途中调用两次拷贝构造函数和析构函数(因为return的是个新的对象)如果采用String& operator+(const String& str) 这样就不会有多余的调用(因为这里直接return一个已经存在对象的引用)如果采用return对象
那么第二次赋值运算调用的情况就是将一个新的String对象传递到operator+()的参数中去 相当于 const String&str = returnStringO如果采用return对象引用 那么第二次赋值运算的情况就是将一个已经存在的String对象(其实就是str1)的引用传递到operator+()的参数中去 const String&str = returnR (String& returnReference = str1;)+=等运算符也是同样的考虑,比如int main()
String str1("abc");
String str2("123");
String str3("456");
str1.show();
str2.show();
str3.show();
str3 = str1 = str2;//str3.operator=(str1.operator=str2)
str3.show();
str1.show();
int num = 10;
num+=(num+=100);
cout&&num&&
如果要这样使用+=或其他上面举出的运算符,+=运算的返回值一定要是一个对象或者引用才行,不然就会出现错误(参数类型不符号)。当然如果确定不会这么使用,直接返回void也是可以的上面的例子也说明了一点,析构函数的调用是在变量作用域结束的时候(以及程序运行结束的时候)在不需要对返回值进行操作的时候,直接返回void就行了,首先要明白一点,运算符左侧的对象就是操作对象比如ObjectA = ObjectB
ObjectA.operator=(ObjectB) ObjectA+=ObjectB
ObjectA.operator+(ObjectB)下面看返回voidpublic:
String(const char* s);
void operator=(const String& another);
void String::operator=(const String &other)
if(this == &other)
len = other.
str = new char[len + 1];
strcpy(str,other.str);
int main()
String str1("abc");
String str2("123");
str1.show();
str2.show();
str1 = str2;//str1.operator=str2
str1.show();
返回void也是可以成功完成赋值运算符赋值的以上就是答案232 条评论分享收藏感谢收起关于c++运算符重载中形参为引用的报错_c++吧_百度贴吧
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&签到排名:今日本吧第个签到,本吧因你更精彩,明天继续来努力!
本吧签到人数:0成为超级会员,使用一键签到本月漏签0次!成为超级会员,赠送8张补签卡连续签到:天&&累计签到:天超级会员单次开通12个月以上,赠送连续签到卡3张
关注:299,093贴子:
关于c++运算符重载中形参为引用的报错收藏
Complex& operator+=(Complex&);Complex& Complex::operator+=(Complex &c){Real+=c.RImage+=c.Ireturn *}int main(void){Complex c1(25,50),c2,c3(100,200),c4(200,400),c5,c;c1.Show();c2=c1;c=c1+c3;c.Show();c+=c1;c.Show();c4+=c1+c2; c4.Show();
c4+=200+c4; c4.Show();c5=-c4; c5.Show();Complex aad=c1+10;return 0;}/home/blocks/codeblocks/TEST/main.cpp|59|error: no match for ‘operator+=’ (operand types are ‘Complex’ and ‘Complex’)|/home/blocks/codeblocks/TEST/main.cpp|59|note: candidate is:|/home/blocks/codeblocks/TEST/main.cpp|44|note: Complex& Complex::operator+=(Complex&)|/home/blocks/codeblocks/TEST/main.cpp|44|note:
no known conversion for argument 1 from ‘Complex’ to ‘Complex&’|比如这个,主函数中+=运算都报错完整源码见一楼
c++学习?选海同,先工作后付款!海同专注c++学习,口碑更好!报名海同c++学习免费赠送价值8000元的嵌入式课程!
#include &iostream&class Complex{float Real,Ipublic:Complex(float r=0,float i=0){Real=r;Image=i;}float& AcessR(){return R}float& AccessI(){return I}void Show(){cout&&Rif(Image&=0) cout&&&\t+&;else cout&&&\t&;cout&&Image&&&i\n&;}Complex& operator+=(Complex&);friend Complex operator+(Complex&,Complex&);friend Complex operator+(Complex, float);friend Complex operator+(float,Complex);friend Complex operator-(Complex);};Complex operator+(Complex& c1,Complex& c2){Ct.Real=c1.Real+c2.Rt.Image=c1.Image+c2.I}Complex operator+(Complex c,float s){return Complex(c.Real+s,c.Image);}Complex operator+(float s,Complex c){return Complex(c.Real+s,c.Image);}Complex operator-(Complex c){return Complex(-c.Real,-c.Image);}Complex& Complex::operator+=(Complex &c){Real+=c.RImage+=c.Ireturn *}int main(void){Complex c1(25,50),c2,c3(100,200),c4(200,400),c5,c;c1.Show();c2=c1;c=c1+c3;c.Show();c+=c1;c.Show();c4+=c1+c2; c4.Show();
c4+=200+c4; c4.Show();c5=-c4; c5.Show();Complex aad=c1+10;return 0;}
登录百度帐号

我要回帖

更多关于 运算符重载实验报告 的文章

 

随机推荐