Vectors, oh my!

For some reason, when I try to initialize a vector I end up getting an "operator=" error which I can't seem to figure out. I'm still fairly new to C++, so there's a lot to learn. Anyway, here's my header and source file:

source:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
Board::Board(unsigned int numberOfRows, unsigned int numberOfColumns)
{
    this->m_numRows = numberOfRows;
    this->m_numColumns = numberOfColumns;
    this->m_boardValues = new std::vector<unsigned int>();

    for (int i = 0; i < this->m_numRows; i++)
    {
        this->m_boardValues.push_back(vector<unsigned int>(m_numberOfColumns, 0));
    }
}

void Board::generate()
{
    srand(1);

    for(int i = 0; i < this->m_numRows; i++)
    {
        bool atMid = false;

        if (i == this->m_numRows / 2)
        {
            atMid = true;
        }

        for(int j = 0; j < this->m_numColumns; j++)
        {
            if (atMid)
            {
                std::cout << "FREE SPACE" << std::endl;
                break;
            }

            this->m_boardValues[i][j] = rand() % 10 + j;

            printf("%d", m_boardValues[i][j]);
        }

        std::cout << "\n";
    }
}


Header:

1
2
3
4
5
6
7
8
9
10
11
class Board
{
public:
    Board(unsigned int numberOfRows, unsigned int numberOfColumns);
    void generate();

private:
   std::vector<std::vector<unsigned int> > m_boardValues;
   unsigned int m_numRows;
   unsigned int m_numColumns;
};


Error:
/home/holland/Code/cpp/Bingo-build-desktop-Desktop_Qt_4_7_4_for_GCC__Qt_SDK__Release/../Bingo/board.cpp:10: error: no match foroperator=’ in ‘((Board*)this)->Board::m_boardValues = (operator new(24u), (<statement>, ((std::vector<unsigned int>*)<anonymous>)))’

Thanks for the help.
This:
new std::vector<unsigned int>();
allocates heap memory for your vector, and returns a pointer to that memory (i.e. pointer to std::vector<unsigned int>). And your try to assign it to a non pointer (i.e. std::vector<unsigned int>):
this->m_boardValues = new std::vector<unsigned int>();

You could make the data member a pointer to an std::vector<unsigned int>, but as far as I can see, just leave the data member the same and remove this line:
this->m_boardValues = new std::vector<unsigned int>();
You seem to be trying to call the default constructor of a class type member variable, an action which is automatically added by the compiler at the start of your class' constructor.
Topic archived. No new replies allowed.