【问题标题】:Error must have class type错误必须具有类类型
【发布时间】:2023-03-24 00:26:02
【问题描述】:

我是 C++ 的新手,在尝试编译此代码时,我得到了一个我不知道如何修复的错误:

int main()
{
    typedef pair<int,int> nodo;
    int x;
    cin >> x; 
    int *g;                
    g = new int[x];   

    vector <nodo> g;


    g[1].push_back(nodo(2,5));
    g[1].push_back(nodo(3,10));
    g[3].push_back(nodo(2,12));
    g[2].push_back(nodo(4,1));
    g[4].push_back(nodo(3,2));

    for (int i = 1; i <=4; ++i){
        //    cout << i << " -> ";
        for (int j = 0; j<g[i].size(); ++j){
            //    cout << g[i][j].first << " c: " << g[i][j].second << " ";    
        }
        //   cout << endl;
    }

    dijkstra(1, x);
    system("pause");
    return 0;
}

我收到的错误是:

Error: Expression must have a class type.

【问题讨论】:

  • 你从哪里得到这个错误?
  • 不要使用system("pause");...just don't
  • @EtiennedeMartel 我在这里收到错误:g[1].push_back(nodo(2,5)); g[1].push_back(nodo(3,10)); g[3].push_back(nodo(2,12)); g[2].push_back(nodo(4,1)); g[4].push_back(nodo(3,2));
  • @Volkanİlbeyli 这有什么问题?是 暂停吗?
  • @Abyx 类 Unix 系统没有。

标签: c++ types expression


【解决方案1】:

所以这个版本可以编译,我认为这就是你要做的:

// Need to include these headers
#include <utility>
#include <iostream>
#include <vector>

using namespace std;

int main()
{
    typedef pair<int,int> nodo;
    int x;
    cin >> x; 
    //int *h;                
    //h = new int[x];   

    //specify size of vector
    std::vector< std::vector<nodo> > g(x);

    g[0].push_back(nodo(2,5));
    g[1].push_back(nodo(3,10));
    g[2].push_back(nodo(2,12));
    g[3].push_back(nodo(4,1));
    g[4].push_back(nodo(3,2));


    for (int i = 0; i < g.size(); ++i){
        std::cout << i << " -> ";
        for (int j = 0; j<g[i].size(); ++j){
                cout << g[i][j].first << " c: " << g[i][j].second << " ";    
        }
         cout << endl;
    }

    //dijkstra(1, x);
    //system("pause");
    return 0;
}

许多问题,您使用g 两次为一。我不确定你想用vector 做什么,但也许你想要vectors 中的vector,它会更像这样:

 std::vector< std::vector<nodo> > g(x) ;

那么这会更有意义:

 g[0].push_back(nodo(2,5)) ;

vector 的第一个元素将位于 0 而不是 1

【讨论】:

    【解决方案2】:

    这里:

    int *g;
    g = new int[x];
    vector <nodo> g; // ERROR: Redeclaration!
    

    您首先将g 声明为int* 类型,然后将其重新声明为vector&lt;nodo&gt; 类型。这是非法的。

    此外,如果您想省略标准命名空间中类型的std:: 限定条件,则需要有一个using namespace std 指令。我不建议你使用它。更好地明确指定std::,或者更确切地说使用特定的using 声明。

    例如:

        typedef std::pair<int,int> nodo;
    //          ^^^^^
        int x;
        std::cin >> x;
    //  ^^^^^
        int *g;
        g = new int[x];
    
        std::vector <nodo> g;
    //  ^^^^^
    

    还要确保您正在导入所有必要的标准标头:

        Type     |  Header
    --------------------------
    std::vector -> <vector>
    std::pair   -> <utility>
    std::cin    -> <iostream>
    

    【讨论】:

      【解决方案3】:

      你有两个名为g的东西:

      int* g;
      

      vector <nodo> g;
      

      这甚至无法编译。

      看起来你想要一个向量数组,在这种情况下你需要类似的东西

      std::vector<std::vector<nodo> > g(x); // size x vector of vectors.
      

      那么你可以做这种事情:

      g[1].push_back(nodo(2,5));
      g[1].push_back(nodo(3,10));
      

      【讨论】:

        【解决方案4】:

        pair 不是一个类,因为你没有包含&lt;utility&gt;

        您还没有包含&lt;vector&gt;&lt;iostream&gt;

        【讨论】:

        • 根据他给出的代码,他也没有包含&lt;iostream&gt;&lt;vector&gt;
        • 谢谢,将它们添加到我的答案中。
        【解决方案5】:

        您要重新声明 g,首先它是 int*,然后您将其变成 vector&lt;int&gt;。我不确定它是如何通过编译器的。

        另外,不要使用nodo(1,2),而是考虑使用make_pair。使用 new 也被认为是不好的做法,您应该使用像 std::vector 这样的动态容器或像 std::array 这样的静态容器。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2017-07-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-09-26
          相关资源
          最近更新 更多