c++中的命名空间类模板继承
namespaced class template inheritance in C++
在之前的问题中,我问了c++中的类模板继承问题。
我现在有一个额外的级别添加!
考虑下面的代码。(假设成员定义存在且准确)
namespace Game
{
namespace Object
{
template<typename T>
class Packable
{
public:
/**
* Packs a <class T> into a Packet (Packet << T)
* Required for chaining packet packing
*************************************************/
virtual sf::Packet& operator <<(sf::Packet& packet) = 0; // Work-horse, must be defined by child-class
friend sf::Packet& operator <<(sf::Packet& packet, T &t);
friend sf::Packet& operator <<(sf::Packet& packet, T *t);
/**
* Unpacks a <class T> from a Packet (Packet >> T)
* Required for chaining packet unpacking
*************************************************/
virtual sf::Packet& operator >>(sf::Packet& packet) = 0; // Work-horse, must be defined by child-class
friend sf::Packet& operator >>(sf::Packet& packet, T &t);
friend sf::Packet& operator >>(sf::Packet& packet, T *t);
/**
* Unpacks a <class T> from a Packet (T <<= Packet)
* Returns the <class T> for convienence
*************************************************/
//friend T& operator <<=(T t, sf::Packet& packet); // Returning reference to cut down on copying (they're already passing us our own copy)
friend T& operator <<=(T &t, sf::Packet& packet);
friend T* operator <<=(T *t, sf::Packet& packet);
};
}
}
这个Ship类继承自Game::Object::Packable
class Ship : public Game::Object::Base<Ship>, public Game::Object::Packable<Ship>
{
public:
Ship( void );
//...
// For packing and unpackinng packets
sf::Packet& operator <<(sf::Packet& packet);
sf::Packet& operator >>(sf::Packet& packet);
}
我们剩下的是以下错误:
(null): "Game::Object::operator<<(sf::Packet&, Ship*)", referenced from:
我得出的结论是,这一定与名称空间的使用有关。如果我想保留名称空间,解决方案是什么?
下面是方法定义的摘录。我必须要dereference
命名空间吗?(我不认为这意味着什么哈哈)
/**
* Packs a <class T> into a Packet (Packet << T)
* Required for chaining packet packing
*************************************************/
template<class T>
sf::Packet& operator <<(sf::Packet& packet, T *t)
{
// Call the actual one, but basically do nothing... this needs to be overrided
return packet << *t;
}
template<class T>
sf::Packet& operator <<(sf::Packet& packet, T &t)
{
// Call the pointer one, but basically do nothing... this needs to be overrided
return packet << &t;
}
// ... other definitions etc.
友元声明(非模板非成员)与友元函数模板不匹配。我建议您在类定义中提供实现:
template<typename T>
class Packable
friend sf::Packet& operator <<(sf::Packet& packet, T &t) {
return packet << t;
}
//...
允许编译器根据需要生成一个自由的非模板函数。其他可选的方法还包括对你关心的模板或模板专门化进行友化。
当然,你可以完全忽略友谊,只在命名空间级别提供模板,因为它们是通过public函数实现的…相关:
- https://stackoverflow.com/a/23718008/36565
- https://stackoverflow.com/a/4661372/36565
相关文章:
- 命名空间中具有.h和.cpp文件的类
- 从父命名空间重载类型
- 在命名空间中定义函数还是限定函数
- C++:对不存在的命名空间使用命名空间指令
- 通过继承类使用来自不同命名空间的运算符
- 使用命名空间时出现多个定义错误
- CUDA内核和数学函数的显式命名空间
- 嵌套的匿名命名空间
- 有关通过派生类在命名空间内继承的问题
- 对"vtable for "命名空间继承的未定义引用 对"类型信息"的未定义引用
- 模板类继承命名空间的名称查找
- 继承重载函数:需要命名空间
- 从类继承,在不同的命名空间中定义
- 命名空间继承
- 使用从命名空间继承的类的Eclipse相对缩进
- D&D 游戏的类、命名空间和继承帮助
- c++,命名空间,对继承类和Qt的名称查找
- c++中的命名空间类模板继承
- 命名空间和继承
- c++:从类继承是否会将其带入命名空间