【发布时间】:2016-08-04 18:23:56
【问题描述】:
这是课程
class graph {
public:
graph() {}; // constructor
graph(int size);
friend ostream& operator<< (ostream& out, graph g);
private:
int size;
bool** grph;
};
这就是我生成图表的方式:
graph::graph(int size) {
grph = new bool*[size];
for (int i = 0; i < size; ++i)
grph[i] = new bool[size];
for (int i = 0; i < size; ++i)
for (int j = i; j < size; ++j) {
if (i == j)
grph[i][j] = false;
else {
cout << prob() << endl;//marker
grph[i][j] = grph[j][i] = (prob() < 0.19);
cout << grph[i][j] << endl;//marker
}
}
cout << "Graph created" << endl;//marker
}
构造函数和 prob() 函数工作得很好。我已经使用标记对它们进行了测试。
这是我认为存在问题的地方。这是重载运算符 的代码
ostream& operator<< (ostream& out, graph g) {
for (int i = 0; i < g.size; ++i) {
for (int j = 0; j < g.size; ++j)
out << g.grph[i][j] << "\t";
out << endl;
}
return out;
}
这是如何调用的。
graph g(5);
cout << g << endl;
现在,程序编译得很好。但是,在执行时,图表没有被打印出来。我已经能够在不重载运算符的情况下以相同的方式打印图形,但是通过在 main 内部运行 for 循环或使用类成员函数。
谁能帮帮我?我正在使用 Visual Studio 2015。
【问题讨论】:
-
这个类还需要重载一些函数来适应复制。这篇文章应该很有用:stackoverflow.com/questions/4172722/what-is-the-rule-of-three
标签: c++ class operator-overloading runtime-error graph-theory