C ++ constructor options

So, I wrote some C ++ today after a long break for a simple coordinate system, and I decided that I want the constructor to take 2 values ​​so that I can 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: there is no corresponding function to call `Coordinates :: Coordinates () 'adventure.cpp: 22: note: candidates: Coordinates :: Coordinates (const Coordinates &) adventure. cpp: 26: note: Coordinates :: Coordinates (int, int)

Not sure what is going on here, and I cannot find much information about C ++ constructors. Any ideas?

+3
source share
2 answers

36 ( ) , - . , ints . , , a b .

+8

, , , Coordinates coords;. :

struct Coordinates {
  int x;
  int y;

  Coordinates(int a, int b) {
    x = a;
    y = b;
  }

  Coordinates(): x(0), y(0) {}
};

, - ; , , undefined , .

, ( : x(0), y(0)) . , (, const ).

, ++ 11 , ,

Coordinates() = default;

.

+4
source

All Articles