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?
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
aandbto the constructor.