gpt4 book ai didi

c++ - 没有可调用的匹配函数

转载 作者:行者123 更新时间:2023-11-28 03:06:46 24 4
gpt4 key购买 nike

1.使用重载运算符+的复制构造函数时出错
2.当从代码中删除复制构造函数时它工作正常

#include <iostream>
#include <cstdlib>
#include <ctime>


using namespace std;
class number {
int n,p;
public:
number () {
n= random ();
p = random()+random();
cout << "constructor with random called"<< endl;
}
number (int n1, int p1) {
n =n1;
p=p1;
cout << "Constructor with value called"<< endl;
cout << "n ="<< n<< endl;
cout << "p = " << p<< endl;
}
int random () {
srand (time(0));
int tmp =rand()%100;
return (tmp);
}
number operator +(number &obj1) ;
friend ostream& operator << (ostream &out,number &obj) ;
number (number &obj) {
n= obj.n;
p=obj.p;
}
} ;
number number::operator+(number &obj1) {
int tmp1 = n+obj1.n ;
int tmp2 = p+obj1.p;
return number (tmp1,tmp2);
}

int main(int argc, char **argv)
{ number n1;
number n2;
cout<< n1<< endl;
cout<< n1<< endl;
//cout << n1+n2<< endl;
return 0;
}

1) 使用重载运算符的复制构造函数时出错 +
2)当从代码中删除复制构造函数时它工作正常

最佳答案

您的复制构造函数采用左值,而默认生成的构造函数采用右值。

改变你的构造函数:

number (const number &obj)
// ^^^^^

但我认为没有理由在这里使用用户定义的复制构造函数。

关于c++ - 没有可调用的匹配函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19475393/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com