I have the following data-structure as a class named “Task”:
private:
string name;
int computation_time;
int period;
Furthermore i have a ASCII-File with this content:
A 3 10
B 2 12
C 1 11
name = A, computation_time = 3, period = 10 and so on….
Now i want to read in the file, create Task-object and push it back into a vector:
void read_in_task_list_and_create_tasks(const string &filename, vector<Task> ¤t_tasks)
{
ifstream in_file;
in_file.open(filename.c_str());
string tmp_name;
int tmp_computation_time;
int tmp_period;
while(!in_file.eof())
{
in_file >> tmp_name;
in_file >> tmp_computation_time;
in_file >> tmp_period;
// Task tmp_task(tmp_name, tmp_computation_time, tmp_period);
// current_tasks.push_back(tmp_task);
current_tasks.push_back(Task(tmp_name, tmp_computation_time, tmp_period));
}
}
Now, when i take a look into current_tasks vector, it has elements, but their values dont match my in_file values.
Watch the outcommented lines. tmp_task object is exactly correct, but if it’s getting pushed back, it loses it’s values like descriped above.
Could this be a Copy-Constructor Issue in Task-class, because std::vector is managing the memory-allocation?
I’m using netbeans with g++ compiler on Linux x86.
THX
Make sure there are no copy constructors or assignment operators defined.
The automatic ones should do exactly what you want.