代码在编译时生成三个错误
Code generates three errors while compiling
我的任务是编写一个程序来查找源节点的最长路径。我使用了Dial的算法,收到了三个错误。第一个是第 58 行'this' cannot be used in a constant expression
list B[W * V + 1];
指的是V
.第二个错误是expression did not evaluate to a constant
,调用与之前相同的线路。 第三个错误是'.push_back' must have class/struct/union
调用第 60 行B[0].push_back(src)
;
我对C++仍然相当陌生,所以任何帮助都非常感谢。
这是完整的代码:
#include "stdafx.h"
#include <iostream>
#include <queue>
#include <list>
# define INF 0x3f3f3f3f
using namespace std;
struct Node
{
int key;
struct Node* left, *right;
};
struct Node* newNode(int key)
{
struct Node* temp = new Node;
temp->key = key;
temp->left = temp->right = NULL;
return temp;
}
class Graph
{
int V;
list< pair<int, int> > *adj;
public:
Graph(int V);
void addEdge(int u, int v, int w);
void longestPath(int s, int W);
};
Graph::Graph(int V)
{
this->V = V;
adj = new list< pair<int, int> >[V];
}
void Graph::addEdge(int u, int v, int w)
{
If (v < V && w < V)
{
adj[u].push_back(make_pair(v, w));
adj[v].push_back(make_pair(u, w));
}
}
void Graph::longestPath(int src, int W)
{
vector<pair<int, list<int>::iterator> > dist(V);
for (int i = 0; i < V; i++)
dist[i].first = INF;
vector<list<int>> B(W * V + 1);
B[0].push_back(src);
dist[src].first = 0;
int idx = 0;
while (1)
{
while (B[idx].size() == 0 && idx < W*V)
idx++;
if (idx == W*V)
break;
int u = B[idx].front();
B[idx].pop_front();
for (auto i = adj[u].begin(); i != adj[u].end(); ++i)
{
int v = (*i).first;
int weight = (*i).second;
int du = dist[u].first;
int dv = dist[v].first;
if (dv > du + weight)
{
if (dv != INF)
B[dv].erase(dist[v].second);
dist[v].first = du + weight;
dv = dist[v].first;
B[dv].push_front(v);
dist[v].second = B[dv].begin();
}
}
}
printf("Longest Distance from Sourcen");
for (int = 0; I < V; ++i)
printf("%d &dn", i, dist[i].first);
}
int main()
{
struct Node* root = newNode(27);
root->left = newNode(14);
root->left->left = newNode(10);
root->left->right = newNode(19);
root->right = newNode(35);
root->right->left = newNode(31);
root->right->right = newNode(42);
int V = 7;
Graph g(V);
g.addEdge(27, 14, 7);
g.addEdge(27, 35, 8);
g.addEdge(14, 10, 1);
g.addEdge(14, 19, 3);
g.addEdge(35, 31, 4);
g.addEdge(35, 42, 2);
g.longestPath(0,8);
return 0;
}
您正在越界访问:
void Graph::addEdge(int u, int v, int w)
{
adj[u].push_back(make_pair(v, w));
adj[v].push_back(make_pair(u, w));
}
这将导致未定义的行为。但我很幸运能得到一个段错误。
您可以添加边界检查来修复它:
if(v < V && w < V){
adj[u].push_back(make_pair(v, w));
adj[v].push_back(make_pair(u, w));
}
您的代码可以编译,但在 gcc 上有一些严重的警告:
这里不需要分号:
struct Node* newNode(int key)
{
struct Node* temp = new Node;
temp->key = key;
temp->left = temp->right = NULL;
return temp;
};
^^^^
这不是有效的C++代码:
list<int> B[W * V + 1];
与 C 不同,C++不允许使用可变长度数组,并且根据编译的不同,您可能会收到错误或警告,因为某些编译器接受此类数组作为C++标准的扩展。这将使您的代码不可移植。
您可以将其更改为:
vector<list<int>> B(W * V + 1);
可以使用调试器在代码中查找这些内容。实时调试
对于list B[W * V + 1]
,c 样式数组的长度不是由常量定义的,因此它会产生一个可变长度数组。可变长度数组是 C99 的功能,在 c++ 中无效。
仅当W * V + 1
始终可以在编译时计算时,list B[W * V + 1]
才有效。
这就是前两个错误的意义所在。第三个错误是后续错误,因为B
无效,push_back
也无效。
在 c++ 中,您希望避免使用 c 样式数组,而是使用std::vector
或std::array
。不能使用std::array
W * V + 1
因为不是恒定的。所以你需要使用std::vector
.
std::vector<list<int>> B(W * V + 1);
- 模板-模板参数推导:三个不同的编译器三种不同的行为
- 在 2D 向量中使用第三个 [ ] 有什么意义?
- 如何通过按下第三个窗口中的按钮,将QString从一个窗口获取到另一个窗口
- 如何知道n!是否可以表示为三个连续数字的乘法?
- 我有三个 getline,但是一旦编译,输入就太多了
- 键盘不起作用 - Arduino Uno - 与变量有关的多个错误
- 使用 g++ 编译时出现多个错误?
- 我遇到了黑客排名中的问题"TWO STRINGS"的三个测试用例的分段错误。原因是什么?
- 有没有更好的方法对C++中的三个整数进行排序?
- 如何检查第三个 API 是否在 Linux 中为 c/c++ 程序创建了一个新线程?
- 即使有 0 个错误,Getter 似乎也会使程序崩溃
- 为什么 getch 在按下函数或箭头键时返回三个值?
- 使用三个数字比较器进行排序
- 代码在编译时生成三个错误
- 在第三个 pary 函数中使用矢量引用时出现意外错误
- 运行时间错误:程序跳过提示,以获取第二名和第三个名称
- 编译三个C++文件.链接错误
- 当第三个模板参数不是std::mbstate_t时,专门处理codecvt:链接器错误
- 在map中传递三个参数显示错误
- 在斯特劳斯特鲁普的PPP2e第7章钻头中找不到第三个错误