gpt4 book ai didi

c++ - 在类模板中专门化类模板?

转载 作者:行者123 更新时间:2023-11-30 03:07:54 27 4
gpt4 key购买 nike

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

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 ​​error C2228: left of '.getKey' must have class/struct/union c:\documents\visual studio 2010\projects\socialnetwork\socialnetwork\graph.h 46 1 SocialNetwork

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

更新:按照 2 位将 >> 更改为 >> 的海报的建议进行了更正。没有不同。错误仍然存​​在。

最佳答案

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

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

关于c++ - 在类模板中专门化类模板?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5525742/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com