代码在编译时会产生三个错误

时间:2019-09-14 06:31:18

标签: c++

我受命编写一个程序来查找从源节点开始的最长路径。我使用了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 Source\n");
    for (int = 0; I < V; ++i)
        printf("%d        &d\n", 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;
}

2 个答案:

答案 0 :(得分:1)

您正在访问以下区域:

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));
}

您的代码compiles,但在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);

您可以使用调试器在代码中查找这些内容。 Live debug

答案 1 :(得分:0)

对于list B[W * V + 1],c样式数组的长度不是由常量定义的,因此它会导致长度可变的数组。可变长度数组是C99的功能,在c ++中无效。

list B[W * V + 1]仅在始终可以在编译时评估W * V + 1时才有效。

这就是前两个错误的所在。第三个错误是后续错误,因为B无效,其上的push_back也无效。

在c ++中,无论如何都要避免使用c样式数组,而应使用std::vectorstd::array。您不能使用std::array,因为W * V + 1不是常数。因此,您需要使用std::vector

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