[英]C++ no matching function for call
I have error: No matching function for call to 'Goo::Goo()' 我有错误:没有匹配的函数可以调用'Goo :: Goo()'
This problem is happening to often, can somebady explain to me where do i make mistakes all the time. 这个问题经常发生,我可以一直向我解释在哪里犯错。 I How can i overcome this.
我我该如何克服。
Here is the code of the progam: 这是程序的代码:
#include <iostream>
using namespace std;
class Goo{
private:
int a[10];
int n;
public:
Goo(int x){
n=x;
}
Goo(const Goo &g){
this->n=g.n;
for(int i=0;i<g.n;i++){
this->a[i]=g.n;
}
}
Goo operator=(const Goo &g){
this->n=g.n;
for(int i=0;i<g.n;i++){
this->a[i]=g.n;
}
return *this;
}
Goo operator+(const Goo &g){
Goo goo;
for(int i=0;i<g.n;i++){
goo.a[i]=this->a[i]+g.a[i];
}
return goo;
}
friend istream& operator>>(istream &in,Goo &g){
in>>g.n;
for(int i=0;i<g.n;i++){
in>>g.a[i];
}
return in;
}
friend ostream& operator<<(ostream &out,Goo &g){
for(int i=0;i<g.n;i++){
out<<g.a[i]<<" ";
}
return out;
}
};
int main()
{
Goo A,B;
cin>>A>>B;
Goo C=A+B;
cout<<C;
return 0;
}
When you define a custom constructor (among other reasons ), the class no longer has a default constructor: 定义自定义构造函数时( 其他原因 ),该类不再具有默认构造函数:
struct Foo {
int x;
};
Foo foo; // OK
struct Foo {
int x;
Foo(int x_) : x{x_} { }
};
Foo foo; // error
You can fix this by either adding a custom default constructor: 您可以通过添加自定义默认构造函数来解决此问题:
struct Foo {
int x;
Foo() { }
Foo(int x_) : x{x_} { }
};
or having at least one constructor with all default parameters: 或至少具有一个带有所有默认参数的构造函数:
struct Foo {
int x;
Foo(int x_ = 0) : x{x_} { }
};
Since C++11, you can also force the compiler to emit the default constructor: 从C ++ 11开始,您还可以强制编译器发出默认构造函数:
struct Foo {
int x;
Foo() = default;
Foo(int x_ = 0) : x{x_} { }
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.