需要帮助,我不明白为什么以下代码没有被编译

发布于 2024-09-25 23:04:01 字数 934 浏览 0 评论 0原文

头文件是“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

笑红尘 2024-10-02 23:04:01

有两个问题。

首先,您必须限定 insert 中的依赖类型:

void insert(GRAPHVERTEX inSRC, GRAPHVERTEX inDST)
{
    typename GRAPHMAP::iterator itr = m_graph.find(inSRC);
}

其次,您需要一个 insert 。顶点类的运算符。在 VERTEX 的公共部分添加以下内容:

bool operator<(const VERTEX<T>& right) const { return m_vertex < right.m_vertex; }

作为风格注意,在 C++ 中,所有大写名称通常保留给常量。 Vertex 对于你的类来说是一个更正常的名称。另请注意,在标头中使用 using namespace 可能会产生许多不良且不可预测的结果,具体取决于包含顺序,应完全避免。

编辑:至少当我用 g++ 编译它时,我得到的第一个错误是关于 GRAPHMAP::iterator 的。当编译器看到一个可以被视为变量或类型的标识符时,它默认选择将其解释为变量,但后来发现它实际上是一种类型。您可以使用 typename 关键字告诉编译器它确实是一种类型。

第二件事要注意的是 map 是一个有序容器,因此您需要传入比较函数或提供 mapmap 的键的运算符。由于 VERTEX 是映射键,因此我设置了一个运算符,以便可以对对象进行排序并保持顺序。随着 VERTEX 类的发展,您可能需要调整比较运算符。

There are two problems.

First you have to qualify the dependent type in insert:

void insert(GRAPHVERTEX inSRC, GRAPHVERTEX inDST)
{
    typename GRAPHMAP::iterator itr = m_graph.find(inSRC);
}

Second, you need a < operator for your vertex class. In the public section of VERTEX add this:

bool operator<(const VERTEX<T>& right) const { return m_vertex < right.m_vertex; }

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 having using 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 the map. Since VERTEX is the map key, I set up an operator< so that the objects can be sorted and have an order maintained. You may need to adjust the comparison operator as your VERTEX class evolves.

叹倦 2024-10-02 23:04:01

您无法使用 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.

乖乖兔^ω^ 2024-10-02 23:04:01

您的 VERTEX 类用作地图中的键;需要定义一个“less”运算符。

Your VERTEX class is used as a key in the map; it is required to have a "less" operator defined.

不必了 2024-10-02 23:04:01

要使用类作为映射中的键,您必须为该类型定义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 implement operator<. 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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文