Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ : Connecting a member of a class to its definition

Tags:

c++

I'm new to C++, I have more experience in OCaml and Python. I want to learn C++ by making a program playing "Morpion Solitaire". My beginnings are a bit difficult.

In the following code :

typedef enum {NORTH, NORTHEAST, EAST, SOUTHEAST} direction;

char deltax[4] = { 0, 1, 1, 1};
char deltay[4] = { 1, 1, 0, -1};

class Coords {
 private:
  char x,y;

 public:
  Coords(char xx,char yy){
    x = xx;
    y = yy;
  };

  char get_x() const { return x;}

  char get_y() const { return y;}
};

class Line {
 private:
  Coords orig;
  direction dir;
  Coords newcross;

 public:
  Line(char x1, char y1, direction d, char x2, char y2) {
    orig = Coords(x1,y1);
    dir = d;
    newcross = Coords(x2,y2);
  };

  Coords nthpoint(char n) {
    char x,y;

    x = orig.get_x() + n*deltax[dir];
    y = orig.get_y() + n*deltay[dir];

    return Coords(x,y);
  };
};

the compiler tells me this :

nico@gaston:~/Travail/Cplusplus/morpion++$ g++ -c morpion.cc 
morpion.cc: In constructor ‘Line::Line(char, char, direction, char, char)’:
morpion.cc:29:57: error: no matching function for call to ‘Coords::Coords()’
   Line(char x1, char y1, direction d, char x2, char y2) {
                                                         ^
morpion.cc:29:57: note: candidates are:
morpion.cc:11:3: note: Coords::Coords(char, char)
   Coords(char xx,char yy){
   ^
morpion.cc:11:3: note:   candidate expects 2 arguments, 0 provided
morpion.cc:6:7: note: Coords::Coords(const Coords&)

I don't understand the message. I have given a 2 argument constructor for class Coords, but the compiler keeps telling me that orig = Coords(x1,y1) calls the constructor with 0 arguments.

What did I miss ?

Remark : I initially put the declarations of Coords and Line in different files, and thought I did not use the proper #include, but putting everything in one single file didn't solve the problem...

like image 863
Nicolas FRANCOIS Avatar asked Dec 15 '22 04:12

Nicolas FRANCOIS


2 Answers

Line(char x1, char y1, direction d, char x2, char y2)
  : orig(x1,y1), dir(d), newcross(x2, y2) {}

Coords doesn't have a default constructor. Your original code attempts to first default-construct orig, then assign a new value to it. But for lack of that default constructor, the first step fails.

like image 180
Igor Tandetnik Avatar answered Dec 30 '22 15:12

Igor Tandetnik


The problem you are having is that all class members and basses must be constructed before entering the constructor body. Because of that what your constructor actually looks like after having the implicit constructions added by the compiler is

Line(char x1, char y1, direction d, char x2, char y2): orig(), dir(), newcross() {
    orig = Coords(x1,y1); // this is assignment not construction
    dir = d; // this is assignment not construction
    newcross = Coords(x2,y2); // this is assignment not construction
};

This is not a problem for dir since it can be implicitly constructed but Coords is not so orig() and newcross() fails.

To fix this you need to use the member initialization list and have a constructor like

Line(char x1, char y1, direction d, char x2, char y2) : orig(x1, y1), newcross(x2, y2), dir(d) {}
like image 22
NathanOliver Avatar answered Dec 30 '22 16:12

NathanOliver