I have error: No matching function for call to '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:
struct Foo {
int x;
Foo() = default;
Foo(int x_ = 0) : x{x_} { }
};
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.