在类模板中专门化一个类模板?

发布于 2024-10-30 01:59:20 字数 1426 浏览 1 评论 0原文

下面的定义失败了......我认为它与在另一个类模板(图形)中专门化类模板(向量)有关。谢谢!

this is the part giving me trouble (defined in Graph below) -> 
std::map<KeyType, Vertex<KeyType, ObjectType> > vertexes;

template <class KeyType, class ObjectType>
class Vertex
{
private:
    KeyType key;
    const ObjectType* object;
public:
    Vertex(const KeyType& key, const ObjectType& object);
    const KeyType getKey();
};

template <class KeyType, class ObjectType> 
class Graph
{
private:
    std::map<KeyType, Vertex<KeyType, ObjectType> > vertexes;
public:
    const Vertex<KeyType, ObjectType>& createVertex(const KeyType& key, const ObjectType& object);
};

template <class KeyType, class ObjectType>
const Vertex<KeyType, ObjectType>& Graph<KeyType, ObjectType>::createVertex(const KeyType& key, const ObjectType& object)
{
    Vertex<KeyType, ObjectType> *vertex = new Vertex<KeyType, ObjectType>(key, object);
    vertexes.insert(pair<KeyType, Vertex<KeyType, ObjectType> >(vertex.getKey(), vertex));
    return *vertex;
};

Visual Studio 10 报告:

错误 1 ​​错误 C2228:'.getKey' 的左侧必须具有类/结构/联合 c:\documents\visual studio 2010\projects\socialnetwork\socialnetwork\graph.h 46 1 SocialNetwork

错误中提到的行对应于接近尾声的vertexes.insert 调用。

更新:按照 2 张海报的建议进行了更正,更改了>>到> >。没有区别。错误仍然存​​在。

The definition below is failing... I'm thinking it has something to do with specializing a class template (Vector) within another class template (Graph). Thanks!

this is the part giving me trouble (defined in Graph below) -> 
std::map<KeyType, Vertex<KeyType, ObjectType> > vertexes;

template <class KeyType, class ObjectType>
class Vertex
{
private:
    KeyType key;
    const ObjectType* object;
public:
    Vertex(const KeyType& key, const ObjectType& object);
    const KeyType getKey();
};

template <class KeyType, class ObjectType> 
class Graph
{
private:
    std::map<KeyType, Vertex<KeyType, ObjectType> > vertexes;
public:
    const Vertex<KeyType, ObjectType>& createVertex(const KeyType& key, const ObjectType& object);
};

template <class KeyType, class ObjectType>
const Vertex<KeyType, ObjectType>& Graph<KeyType, ObjectType>::createVertex(const KeyType& key, const ObjectType& object)
{
    Vertex<KeyType, ObjectType> *vertex = new Vertex<KeyType, ObjectType>(key, object);
    vertexes.insert(pair<KeyType, Vertex<KeyType, ObjectType> >(vertex.getKey(), vertex));
    return *vertex;
};

Visual Studio 10 reports:

Error 1 error C2228: left of '.getKey' must have class/struct/union c:\documents\visual studio 2010\projects\socialnetwork\socialnetwork\graph.h 46 1 SocialNetwork

The line mentioned in the error corresponds to the the vertexes.insert call near the end.

UPDATE: made correction as suggested by 2 posters of changing the >> to > >. No difference. Error persists.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

你的往事 2024-11-06 01:59:20

您的顶点是一个指针。要访问 getKey,您需要使用 -> 运算符,而不是 .。另外,您可以使用 std::make_pair 来避免重复类型。

vertexes.insert(std::make_pair(vertex->getKey(), *vertex));

Your vertex is a pointer. To access getKey you need to use -> operator, not .. Plus, you can use std::make_pair to avoid repeating the types.

vertexes.insert(std::make_pair(vertex->getKey(), *vertex));
撞了怀 2024-11-06 01:59:20

由于没有错误消息本身,我无法提供帮助。但如果我可以大胆猜测:

除非您使用 C++0x,否则

std::map<KeyType, Vector<KeyType, ObjectType>> vertexes;

解析将失败,因为末尾的 >> 被解析为运算符,而不是嵌套的末尾模板参数列表。因此,您需要将其更改为

std::map<KeyType, Vector<KeyType, ObjectType> > vertexes;

This does getfixed in C++0x,不过。

Not having the error message itself, I can't be that helpful. But if I may venture a guess:

Unless you are using C++0x,

std::map<KeyType, Vector<KeyType, ObjectType>> vertexes;

will fail parsing, since >> at the end is parsed as the operator, as opposed to the end of a nested template parameter list. So, you'll need to change that to

std::map<KeyType, Vector<KeyType, ObjectType> > vertexes;

This does get fixed in C++0x, though.

安穩 2024-11-06 01:59:20

首先从向量更改为顶点(拼写错误?)。第二个错误是“<<”,如果将它们放在一起,它将是移位运算符。该行应该是这样的:

std::map<KeyType, Vertex<KeyType, ObjectType> > vertexes;

编辑:根据新信息,我可以发现另一个错误:

在方法 createVertex 中,您正在创建一个新的顶点,但将其用作值。例如,在这一行中:

vertexes.insert(std::pair<KeyType, Vertex<KeyType, ObjectType> >(vertex.getKey(), vertex));

调用:

vertex.getKey()

Vertex 是一个指针,因此您应该更改为 vertex->getKey()。
还有更多错误,但都与顶点是被视为值的指针这一事实有关。

First change from Vector to Vertex (typo?). The second error is the "<<", if you put them together it will be the shift operator. Here is how that line should be:

std::map<KeyType, Vertex<KeyType, ObjectType> > vertexes;

Edit: With the new information I could spot another error:

In the method createVertex you are creating a new Vertex but using it as Value. For example, in this line:

vertexes.insert(std::pair<KeyType, Vertex<KeyType, ObjectType> >(vertex.getKey(), vertex));

The call:

vertex.getKey()

Vertex is a pointer, so you should change to vertex->getKey().
There are more errors, but all related to the fact that vertex is a pointer being treated as a value.

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