错误 C2923:'std::vector':'Edge'不是参数"_Ty"的有效模板类型参数?
error C2923: 'std::vector' : 'Edge' is not a valid template type argument for parameter '_Ty'?
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 {
....
};
}
相关文章:
- 欧拉项目#8答案是大以获得有效答案
- 调整大小后指向元素值的指针unordered_map有效?
- 为什么是0;C++中的有效语句
- 最高有效数字侧的第N位
- GCC对可能有效的代码抛出init list生存期警告
- 有效地使用std::unordered_map来插入或增加键的值
- c++中O(n^(1/3))中一个数的除数的有效计数
- 使用无符号字符数组有效存储内存
- 自定义先决条件对移动分配运算符有效吗
- 为什么将值返回函数传递给重载=运算符对运算符函数有效,而对其他运算符无效
- 有哪些有效的方法可以消除一组 100 万个字符串>重复数据?
- 为什么这种直接初始化有效?(C++17)
- 递归函数有效,但无法记忆
- 在C++中初始化向量映射的最有效方法
- 如果变量名称不跟在 char* 后面,const char* 是否有效?
- 钳制迭代器是否有效
- 如何有效地在 std::vector 中插入一对?
- C++模板 t 不是有效的模板类型
- 检查由括号、方括号和大括号组成的一组方括号是否有效?
- unordered_set<reference_wrapper<Ty>>有效吗?