在类模板中专门化一个类模板?
下面的定义失败了......我认为它与在另一个类模板(图形)中专门化类模板(向量)有关。谢谢!
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的
顶点
是一个指针。要访问getKey
,您需要使用->
运算符,而不是.
。另外,您可以使用 std::make_pair 来避免重复类型。Your
vertex
is a pointer. To accessgetKey
you need to use->
operator, not.
. Plus, you can usestd::make_pair
to avoid repeating the types.由于没有错误消息本身,我无法提供帮助。但如果我可以大胆猜测:
除非您使用 C++0x,否则
解析将失败,因为末尾的
>>
被解析为运算符,而不是嵌套的末尾模板参数列表。因此,您需要将其更改为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,
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 toThis does get fixed in C++0x, though.
首先从向量更改为顶点(拼写错误?)。第二个错误是“<<”,如果将它们放在一起,它将是移位运算符。该行应该是这样的:
编辑:根据新信息,我可以发现另一个错误:
在方法 createVertex 中,您正在创建一个新的顶点,但将其用作值。例如,在这一行中:
调用:
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:
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:
The call:
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.