代码在编译时生成三个错误

Code generates three errors while compiling

本文关键字:三个 错误 编译 代码      更新时间:2023-10-16

我的任务是编写一个程序来查找源节点的最长路径。我使用了Dial的算法,收到了三个错误。第一个是第 58 行'this' cannot be used in a constant expressionlist 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::vectorstd::array。不能使用std::arrayW * V + 1因为不是恒定的。所以你需要使用std::vector.

std::vector<list<int>> B(W * V + 1);