需要帮助,我不明白为什么以下代码没有被编译
头文件是“graph.h”
#ifndef _GRAPH_H_
#define _GRAPH_H_
#include <map>
#include <vector>
using namespace std;
template <class T>
class VERTEX
{
public:
VERTEX(T inVertex): m_vertex(inVertex), m_visited(false){}
~VERTEX(){}
private:
T m_vertex;
bool m_visited;
};
template <class T>
class GRAPH
{
public:
GRAPH() {}
~GRAPH(){}
typedef VERTEX<T> GRAPHVERTEX;
typedef vector<GRAPHVERTEX> ADJLIST;
typedef map<GRAPHVERTEX, ADJLIST> GRAPHMAP;
void insert(GRAPHVERTEX inSRC, GRAPHVERTEX inDST)
{
GRAPHMAP::iterator itr = m_graph.find(inSRC);
}
private:
GRAPHMAP m_graph;
};
#endif
,测试文件是
#include "graph.h"
int main( int argc, char**argv)
{
GRAPH<int> *G = new GRAPH<int>();
G->insert(VERTEX<int>(0), VERTEX<int>(2));
return 0;
}
Header file is "graph.h"
#ifndef _GRAPH_H_
#define _GRAPH_H_
#include <map>
#include <vector>
using namespace std;
template <class T>
class VERTEX
{
public:
VERTEX(T inVertex): m_vertex(inVertex), m_visited(false){}
~VERTEX(){}
private:
T m_vertex;
bool m_visited;
};
template <class T>
class GRAPH
{
public:
GRAPH() {}
~GRAPH(){}
typedef VERTEX<T> GRAPHVERTEX;
typedef vector<GRAPHVERTEX> ADJLIST;
typedef map<GRAPHVERTEX, ADJLIST> GRAPHMAP;
void insert(GRAPHVERTEX inSRC, GRAPHVERTEX inDST)
{
GRAPHMAP::iterator itr = m_graph.find(inSRC);
}
private:
GRAPHMAP m_graph;
};
#endif
And test file is
#include "graph.h"
int main( int argc, char**argv)
{
GRAPH<int> *G = new GRAPH<int>();
G->insert(VERTEX<int>(0), VERTEX<int>(2));
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
有两个问题。
首先,您必须限定
insert
中的依赖类型:其次,您需要一个
insert
。顶点类的运算符。在 VERTEX 的公共部分添加以下内容:作为风格注意,在 C++ 中,所有大写名称通常保留给常量。
Vertex
对于你的类来说是一个更正常的名称。另请注意,在标头中使用using namespace
可能会产生许多不良且不可预测的结果,具体取决于包含顺序,应完全避免。编辑:至少当我用 g++ 编译它时,我得到的第一个错误是关于 GRAPHMAP::iterator 的。当编译器看到一个可以被视为变量或类型的标识符时,它默认选择将其解释为变量,但后来发现它实际上是一种类型。您可以使用
typename
关键字告诉编译器它确实是一种类型。第二件事要注意的是
map
是一个有序容器,因此您需要传入比较函数或提供map
。map
的键的运算符。由于 VERTEX 是映射键,因此我设置了一个运算符,以便可以对对象进行排序并保持顺序。随着
VERTEX
类的发展,您可能需要调整比较运算符。There are two problems.
First you have to qualify the dependent type in
insert
:Second, you need a < operator for your vertex class. In the public section of VERTEX add this:
As a matter of style note that in C++ ALL CAPS names are usually reserved for constants.
Vertex
would be a much more normal name for your class. Also note that havingusing namespace
in a header can have many undesired and unpredictable results depending on include order and should be completely avoided.EDIT: At least when I compiled this with g++ the first error I got was regarding GRAPHMAP::iterator. When a compiler sees an identifier that could be treated as a variable or a type, it choose to interpret it as a variable by default, but then at a later point discovered it was actually a type. You tell the compiler that it's really a type by using the
typename
keyword.The second thing to note is that
map
is an ordered container and as such you need to either pass in a comparison function OR provide a < operator for the key of themap
. SinceVERTEX
is the map key, I set up anoperator<
so that the objects can be sorted and have an order maintained. You may need to adjust the comparison operator as yourVERTEX
class evolves.您无法使用 GRAPHVERTEX 作为键创建地图,因为您需要能够将键与运算符 <比较。所以你必须定义这个运算符。
You can't create a map with GRAPHVERTEX as a key because you need to be able to compare the key with operator <. So you must define this operator.
您的 VERTEX 类用作地图中的键;需要定义一个“less”运算符。
Your VERTEX class is used as a key in the map; it is required to have a "less" operator defined.
要使用类作为映射中的键,您必须为该类型定义
operator<
,或者需要在创建映射时指定比较器。假设该类型的对象具有“自然”顺序,您通常希望使用它来实现operator<
。当您需要以多种不同的顺序排列给定类型的对象时,通常会使用单独的比较器对象,并且没有一种顺序实际上比其他顺序更“自然”或占主导地位(例如,在与员工一起工作时,您可能会看到他们通过社会安全号码、姓名和资历的频率大致相同)。To use a class as the key in a map, you have to either define
operator<
for that type, or else you need to specify a comparator when you create the map. Assuming objects of that type have a "natural" order, you normally want to use that to implementoperator<
. You typically use a separate comparator object when you need to arrange objects of a given type in a number of different orders, and no one order is really more "natural" or dominant than the others (e.g., when working with employees, you might look at them about equally often by social security number, name, and seniority).