c++ - C++ 中的命名空间类模板继承

标签 c++ templates inheritance namespaces operator-overloading

在之前的一个问题中,我问了 class template inheritance in 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:

我得出的结论是它一定与 namespace 的使用有关。如果我想保留 namespace ,有什么解决方案?

这是方法定义的摘录。我是否必须取消引用 命名空间? (我认为这甚至没有任何意义哈哈)

/**
 * 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;
        }
    //...

这允许编译器按需生成一个免费的非模板函数。其他替代方案包括与您关心的模板或模板特化成为 friend 。

当然,您可以完全忽略友元,只提供 namespace 级别的模板,因为它们是根据public 函数实现的……


相关:

关于c++ - C++ 中的命名空间类模板继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23729016/

相关文章:

c++ - CMake - 无法链接共享库(子目录)

C++ 模板特化问题

c++ - 在单个接口(interface)后面隐藏多个实现

c++ - 模板函数特化默认参数

java - 限制可以在 Java 中实现接口(interface)的类

javascript - 当 ob1.prototype 未定义时,Object.create() 如何从 ob1.prototype 创建 obj2.__proto__ ?

java - 为什么java和c++中opencv的HoughLines结果不同

c++ - 不能将 float 数组传出函数

c++ - 无法使用 Xcode 9 运行项目,许多新的 C++ 语义问题

c++ - 获取重载函数模板的地址