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

namespaced class template inheritance in C++

本文关键字:继承 命名空间 c++      更新时间:2023-10-16

在之前的问题中,我问了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