【发布时间】:2011-05-12 07:19:26
【问题描述】:
我有以下数据结构作为名为“Task”的类:
private:
string name;
int computation_time;
int period;
此外,我还有一个包含此内容的 ASCII 文件:
A 3 10
B 2 12
C 1 11
name = A,computation_time = 3,period = 10 等等......
现在我想读入文件,创建任务对象并将其推回向量中:
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));
}
}
现在,当我查看 current_tasks 向量时,它有元素,但它们的值与我的 in_file 值不匹配。 观看注释掉的行。 tmp_task 对象是完全正确的,但如果它被推回,它就会失去上面描述的值。
这可能是任务类中的复制构造函数问题,因为 std::vector 正在管理内存分配吗?
我在 Linux x86 上使用带有 g++ 编译器的 netbeans。
谢谢
【问题讨论】:
-
你能发布
Task类的完整定义吗?