错误 C2923:'std::vector':'Edge'不是参数"_Ty"的有效模板类型参数?

error C2923: 'std::vector' : 'Edge' is not a valid template type argument for parameter '_Ty'?

本文关键字:Ty 有效 类型参数 std C2923 vector Edge 错误 参数      更新时间:2023-10-16

Hi...

    #ifndef Node_H
    #define Node_H
    #include <vector>
    #include <stack>
    #include <string>
    #include <iostream>
    #include "Edge.h"
    #include "CongestionMap.h"
    using namespace std;   
    class Node
    {
    public:
        Node(){ visit = false;};
    Node(int id);
    ~Node(); 
    int getID();
    void setLocation(int &row, int &col, GridCell *Gc);;
    void displayList();
    private:
        int row;
        int col;
        int id;
        bool visit;
        int parrent;
        int distance;
        typedef vector<  Edge > adjNodeList;
    };
    #endif 

当我编译项目时,我得到错误项目\节点.h(43(: 错误 C2065: "边缘": 未声明的标识符项目\项目\节点.h(43(:错误 C2923:"std::vector":"Edge"不是参数"_Ty"的有效模板类型参数... 请帮帮我...边缘.h

#ifndef Edge_H
#define Edge_H
#pragma once
#include <vector>
#include <stack>
#include <string>
#include <iostream>
#include "Node.h"
using namespace std;
class Edge
{
public:
    Edge() {};
Edge(Node *firstNode, Node *secNode, int inCost);
~Edge(void);
Node* getDstNode();
Node* getOrgNode();
int   getCost();
private:
Node *orgNode;
Node *dstNode;
int cost;
};
#endif

正如一些评论者所指出的,你有循环引用。 代码按其显示顺序进行分析。

如果我们从 node.h ,早期,它包括 edge.h .

edge.h包括node.h,但由于#ifdef保护和冗余#pragma once,这巧妙地不会做任何事情(它们都实现了相同的目标,因此您可以考虑只坚持一种方法(。

好的,我们将遇到的第一个类定义是 Edge . 太好了,除了它指的是Node,没有人知道那是什么......因为我们仍在包含在node.h中的edge.h代码中。

很可能你的事情以相反的方式发生,edge.h首先被包括在内。 接下来发生的事情是包含node.h,并声明Node,它希望知道Edge是什么,但还没有人看到。

所以你需要使用前向声明,即在声明class Edge之前edge.h,添加一行指示Node是什么:

class Node;

反之node.h,为Edge提供前向声明。 第二个是涵盖某人在包含edge.h之前包含node.h的情况。

例如,如果您在同一文件中声明了它们,您仍然需要执行以下操作:

class Node;  // forward declaration so that compiler knows that 
             // Node is a class when it gets to parsing Edge
class Edge {
...
private:
    Node *orgNode;
};
class Node {
....
};
}