-5

What is the correct way to initialize a constructor of an abstract class? I commented out the constructor in the cc file and it seemed to be working, I would like to understand why.

Board. h Board.h is an abstract class, as it has 2 pure virtual function NOT included here.

class Board {
public:
   Board(size_t width, size_t height)
    : width(width), height(height) {}

   virtual ~Board() {}
....

protected:
//this is line 54
size_t height;
size_t width;
};

My localBoard.h

#include <Board.h>
class LocalBoard : public Board {

public:

  LocalBoard(size_t width, size_t height) :Board(width),Board(height) {}

  ~LocalBoard() {}

 ...
};

LocalBoard.cc

#include <board/LocalBoard.h>
    // commenting this out fixed the error
    //LocalBoard(size_t width, size_t height) {}

error: multiple initializations of constructor

On an another note may someone help me understand what the following warning means, what consequences it has for my program and how to fix it? I think it related to the constructor again.

  ./include/board/Board.h: In constructor ‘Board::Board(size_t, size_t)’:
  ./include/board/Board.h:54:9: warning: ‘Board::width’ will be              initialized    after [-Wreorder]
   size_t width;
cndolo
  • 27
  • 2
  • 8

1 Answers1

0

You already provided an inline implementation of class LocalBoard's ctor in this line:

LocalBoard(size_t width, size_t height) :Board(width),Board(height) {}

You cannot give the same ctor twice (once inline and once in the cc file).

You need to write either

class LocalBoard {
    LocalBoard(size_t width, size_t height) :Board(width, height) {}
};

or

class LocalBoard {
    LocalBoard(size_t width, size_t height);
}

// probably in the cc file:
LocalBoard::LocalBoard(size_t width, size_t height) : Board(width, height)
{
}

Please note that I changed your two base class ctor calls Board(width),Board(height) to one base class ctor call Board(width, height).

Regards the warning you can look at What's the point of g++ -Wreorder?.

Werner Henze
  • 16,404
  • 12
  • 44
  • 69