user1311286
user1311286

Reputation:

Two-Dimensional array, cannot succesfully fill with data.

I am using this class below

template<typename T>
class array_2d 
{
public:
    std::size_t data;
    std::size_t col_max;
    std::size_t row_max;
    std::vector<T> a;

    array_2d(std::size_t col, std::size_t row) : data(col*row), col_max(col), row_max(row), a(data)
    {}

    T& operator()(std::size_t col, std::size_t row) 
    {
        assert(col_max > col && row_max > row);
        return a[col_max*col + row];
    }
};

And initializing it as so

    array_2d<CString> tableData(5, 2);
    for(int r = 0; r < 5; r++)
        for(int c = 0; c < 2; c++)
            tableData(r, c) = "Test";

And keep coming back that I am exceeding the bounds of a vector. I have been trying for hours to get a successful 2-dimensional CString array.

Upvotes: 0

Views: 105

Answers (3)

Bartek Banachewicz
Bartek Banachewicz

Reputation: 39370

tableData(r, c) = "Test";

vs

T& operator()(std::size_t col, std::size_t row)

Surely that can't work.

Upvotes: 1

Kerrek SB
Kerrek SB

Reputation: 476970

Say tableData(c, r) = "Test";.

Upvotes: 3

Nim
Nim

Reputation: 33655

Erm.. your arguments/parameter order are mixed up...

Upvotes: 1

Related Questions