2

I'm making a monopoly game, and I have two arrays of Vectors for coordinates for my 2d array of characters. Below is my board.h where the array is kept in the board class

class Board {
    ...
    Vector propList[40];
    Vector coordList[40];
    ...
public:
    ...
};

I am getting an error running my program in bash when trying to create the executable, displayed below (there are 2 identical errors for each array)

board.cc:15:8: error: constructor for 'Board' must explicitly initialize the member 'propList' which does not have a default constructor
Board::Board() {
       ^
./board.h:17:12: note: member is declared here
    Vector propList[40];

I have all 40 elements initialized in my board constructor as displayed below

propList[0] = Vector(-1, -1);
propList[1] = Vector(73, 51);
...
propList[39] = Vector(81, 46);

coordList[0] = Vector(81, 54);
coordList[1] = Vector(73, 54);
...

I also tried the following

Vector v = (-1, 1);
propList[0] = v;
...

and receive the same error. Does anybody know what is going on and how to fix it?

edit: I forgot to add my vector code. I had to create a constructor since I can't use C++11 initialization on my computer.

vector.cc
#include "vector.h"
Vector::Vector(int x, int y) : x(x), y(y) {}

vector.h
struct Vector {
    Vector(int x, int y);
    int x;
    int y;
};
3
  • Your Vector class requires arguments be passed to the consturctor. Commented Nov 30, 2014 at 23:30
  • Was the "...which does not have a default constructor" part of the error message not clear enough? Vector doesn't have a default constructor. It ether needs one or you need a different initialization approach. Commented Nov 30, 2014 at 23:30
  • @WhozCraig sorry I didn't add my vector code. I needed to create a constructor because I can't use c++11 initialization. Commented Nov 30, 2014 at 23:35

1 Answer 1

1

In your constructor, these are assignments, not initialization. Constructors have an initialization list specifically for this purpose. Unfortunately, you can't initialize arrays like this.

Here's the initialization list for a simple int member:

class MyClass
{
    int myField;

    MyClass() :
        m_myField(1)// initialization
    {
        // right here, myField is 1.
        myField = 2;// assignment.
    }
};

This initializes myField to 1. Then assigns 2, like you're doing in your constructor.

You can't initialize arrays like this, so I would recommend a workaround: A std::vector of Vectors. Yea that's confusing because both are called "vector", but they mean different things. std::vector is a storage container.

std::vector<Vector> propList;

and in your constructor, add Vector objects to the propList

Board::Board()
{
    propList.push_back(Vector(-1, -1));
    propList.push_back(Vector(73, 51));
    ...
}

Another solution is to add a default constructor to Vector. But that's a bit dirty compared to using std::vector<>.


In C++11 it may be possible using aggregate initialization in a constructor initialization list. No, it's not possible, period.

Sign up to request clarification or add additional context in comments.

1 Comment

is this a description of the board or the state of play? A monopoly board is a prime candidate for a static const array of Vector, which can certainly be list-initialised.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.