gpt4 book ai didi

c++ - 方法不能初始化值c++

转载 作者:行者123 更新时间:2023-11-28 06:17:32 25 4
gpt4 key购买 nike

首先我在 Ford Bellman claas 的构造函数中初始化值

FordBellman::FordBellman() {
this->vertexCount=0;
this->vertexFirst=0;
this->edgeCount=0;
this->wage=0;
this->matrix=0;
this->distance=0;
this->predecessor=0;

}

接下来我在 Fordbellman 的 initialize 方法的参数中有引用

void FordBellman::initialize(const AdjacencyMatrix &am)
{
this->vertexCount=am.getVertexCount();
this->vertexFirst=am.getVertexFirst();
this->edgeCount=am.getEdgeCount();
this->wage=am.getWage();
this->matrix=am.getMatrix();
cout << vertexCount;
cout << vertexFirst;
.....
}

在主类中我是这样做的

int main() {
AdjacencyMatrix am;
FordBellman fb;
am.createFromFile("matrix.txt");
fb.initialize(am);

}

如果我调用 fb.initialize(am) 控制台什么都不显示(应该显示 cout )你能告诉我我做错了什么吗?

repo https://github.com/likoms/Graph/

最佳答案

当我试图从关于你的 github 存储库中运行代码时,很遗憾我遇到了段错误。原因在文件 FordBellman.cpp 的第 42 行。您没有分配数组但使用了它。

您没有看到任何内容的原因是您的程序在打印某些内容之前已终止。准确地说,它发生在 am.createFromFile("matrix.txt");

内部

尝试为 predecessor 编写内存分配,我认为您的代码将开始工作。

我有点困惑,您没有收到有关段错误的消息。您使用什么开发环境?

关于c++ - 方法不能初始化值c++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29966233/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com