此代码从文件读取数据,我想将其保存到类图的类型的对象的2d动态数组中 当我尝试检查对象的数组g的内容时它是空的...请帮我将数据保存到数组中。
class graph
{
public:
int index;
int c;
int p1;
int p2;
int s1;
int s2;
int t1;
int t2;
int weight;
bool ready;
graph(int index,int c, int p1, int p2, int s1, int s2,int t1, int t2, int weight,int ready)
{
index = index;
c = c;
p1 = p1;
p2 = p2;
s1 = s1;
s2 = s2;
t1 = t1;
t2 = t2;
weight = weight;
ready = ready;
}
};
这是主要代码
int main(){ char argc[20]; int m,index,c,p1,p2,s1,s2,t1,t2,weight,ready; //graph temp(0,0,0,0,0,0,0,0,0,0); fstream f;
cout << "Input file name: "; cin >> argc; f.open(argc, ios::in); f >> m;
graph **g=new graph*[m]; int i = 1; while (!f.eof()) {
f >> index >> c >> p1 >> p2 >> s1 >> s2 >> t1 >> t2 >> weight >> ready; g[i] = new graph(index, c, p1, p2, s1, s2, t1, t2, weight, ready);
cout<< g[i]->index;
i = i + 1;
} return 0; }
答案 0 :(得分:0)
您的对象初始化问题,简化:
p[label]
在上面的简化案例中,行
label
没有做任何有用的事情。这是自我分配。 LHS上的class graph
{
public:
int index;
graph(int index)
{
index = index;
}
};
不是成员变量。这是争论。成员变量仍然未初始化。
您可以使用:
index = index;
更好的是,我建议使用:
index
对所有成员变量进行更改。