Reputation: 33
So I wrote some C++ today after a long break for a simple coordinate system and I figured I'd want a constructor to take in 2 values so that I could write things like "Coordinates c = new Coordinates(1,2);"
struct Coordinates {
int x;
int y;
Coordinates(int a, int b) {
x = a;
y = b;
}
};
When compiled in Cygwin, I get:
$ g++ -o adventure adventure.cpp adventure.cpp:36: error: no matching function for call to `Coordinates::Coordinates()' adventure.cpp:22: note: candidates are: Coordinates::Coordinates(const Coordinates&) adventure.cpp:26: note: Coordinates::Coordinates(int, int)
Not sure what's going wrong here and I can't find much info on C++ struct constructors. Any ideas?
Upvotes: 3
Views: 12017
Reputation: 19731
By writing an explicit constructor, you disabled the automatic creation of a default constructor, which is used when defining objects without constructor parameters, as in Coordinates coords;
. You have to explicitly supply it like this:
struct Coordinates {
int x;
int y;
Coordinates(int a, int b) {
x = a;
y = b;
}
Coordinates(): x(0), y(0) {}
};
Note that I initialized the member variables in the default constructor; it's not mandatory, but it's a good idea because otherwise they will be undefined even in situation where they would be default-initialized with the compiler-generated construction.
Also note that I used member initializers (the : x(0), y(0)
part) instead of assignments in the constructor body. That's good style, for class objects generally gives better performance, and in some cases is the only way to initialize a member at all (e.g. if it is of const
type or has no default constructor).
Note that in C++11, you can just tell the compiler to generate the same default constructor which it would have generated without the other constructor, by just writing
Coordinates() = default;
in the class definition.
Upvotes: 4
Reputation: 67527
In line 36 of your code (which you are not showing) you are creating an object of this class but you are not passing any arguments to the constructor. The only valid constructors are one that takes two ints, or the default copy constructor. Either add a constructor w/o arguments, or change the code to pass a
and b
to the constructor.
Upvotes: 8