0

I'm trying to resize a vector in the class. I don't know where is the error. Hope you can help me.

This is my code:

class State {
private:
    vector<int> numbers;
    int score;

public:
    int getScore() {
        return score;
    }
    void setScore(int score) {
        this->score = score;
    }
    int getSize () {
        return this->numbers.size();
    }
    void setSize(int size) {
        this->numbers.clear();
        this->numbers.resize(size);
        for (int i = 0; i < size; ++i)
            this->numbers[i].resize(size);
    }
};

When I try to compile, I get this error:

In file included from ./power.cpp:11:
./power.hpp:45:24: error: member reference base type 'value_type' (aka 'int') is not a structure or union
                    this->numbers[i].resize(size);
                    ~~~~~~~~~~~~~~~~^~~~~~~
1 error generated.

Hope you can help me.

Thanks in advance :)

1
  • Welcome to SO! please add your main() routine to make it a Minimal, Complete, and Verifiable example. Btw - your vector<int> is technically not "two-dimensional" , thus you try to resize a plain int luring at numbers[i]. Commented Nov 11, 2015 at 17:43

1 Answer 1

1
vector<int> numbers;

declares a one-dimensional vector. In the line

this->numbers[i].resize(size);

you assume that numbers[i] is itself a vector, which is not the case (it is an int). What you probably want is to declare

vector<vector<int>> numbers;

or "simulate" the 2D vector as a 1D flatten vector (in general faster) but making sure to map from 2D to 1D and vice versa.

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

2 Comments

For mentioning "simulate" [..] as 1D vector ++vote: That vector<vector<....>>> contortionism is a pain in the eye.
Yeah, that's it. Thanks for your help :)

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.