userzizzy
userzizzy

Reputation: 179

cannot access the vector members of a class

I have tried to access the members of a class Part that are vector elements of type integer inside the vector tasks.

#include <iostream>
#include <vector>

using namespace std;

class Part{
    vector<int> tasks;
    public:

    void setTasks(void);
    void getTasks(void);
};
void Part::setTasks(void){
    vector<int>::iterator it;
    int i=1;
    for (it = this->tasks.begin(); it != this->tasks.end(); ++it)
    {
        *it=i;
        i=i+1;
    }
}

void Part::getTasks(void){
    vector<int>::iterator it;
    for (it = this->tasks.begin(); it != this->tasks.end(); ++it)
       cout<<*it<<"\t";
}
int main()
{
    Part one;
    one.setTasks();
    one.getTasks();

    return 0;
}

I am simply trying to access the values and print them yet failing. There is no compilation error. In run-time, nothing is outputted in the terminal. Where is the error?

Upvotes: 3

Views: 1732

Answers (4)

vsoftco
vsoftco

Reputation: 56547

Alternatively, you can use a "back-insert" iterator (#include <iterator>), which internally calls std::vector::push_back, like this:

void Part::setTasks(void){
    auto back_it = std::back_inserter(tasks);
    for(int i = 0; i < 10; ++i)
        *back_it++ = i;
}

This kind of iterator is especially useful in algorithms where your destination size is unknown. Although if you know the size in advance, you should use reserve/resize or specify the size at construction, since push-ing back into a vector can sometimes be slow due to re-allocation.

Upvotes: 0

Praetorian
Praetorian

Reputation: 109119

A default constructed vector has zero size, so the for loop in setTasks is never entered (since the begin() and end() iterators are the same at that point). If you set an initial size to the vector your code will work as intended. For instance, try adding the following at the beginning of setTasks

tasks.resize(10); // sets vector size to 10 elements, each initialized to 0

Another way to write that function would be

#include <numeric>

...

void Part::setTasks(void){
    tasks.resize(10);
    std::iota(tasks.begin(), tasks.end(), 1); // requires C++11
}

You could also set the initial size of the vector in the default constructor of Part if you wanted to. In that case add the following public constructor

Part() : tasks(10)
{}

Yet another way to achieve setting the size upon construction would be

class Part{
    vector<int> tasks = vector<int>(10); // requires C++11

Upvotes: 3

Daniel Le
Daniel Le

Reputation: 1830

Your vector is empty. Try giving it a size. For example, vector<int> tasks(10). See option 3 in this.

Upvotes: 0

Masked Man
Masked Man

Reputation: 11015

The size of your vector is 0 when you call setTasks(). Your iterator doesn't get you into the for loop at all. You need to think about what exactly you want your setTasks() to do. How many elements of the vector did you intend to set? You should either define your vector with that size, or use that many number of push_backs instead to set your vector to the desired value.

Upvotes: 1

Related Questions