C ++中的命名空间类模板继承 [英] namespaced class template inheritance in C++

查看:207
本文介绍了C ++中的命名空间类模板继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在先前的问题中,我问了 C ++中的类模板继承. /p>

我现在要添加一个额外的级别!

请考虑以下代码. (假设成员定义正确且准确)

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 函数实现的.


相关:

In an earlier question, I asked asked class template inheritance in C++.

I now have an extra level to add!

Consider the following code. (Assume the member definitions are present and accurate)

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);
        };
    }
}

And this Ship class inherits from 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);
}

What we're left with is the following error.

(null): "Game::Object::operator<<(sf::Packet&, Ship*)", referenced from:

I've come to the conclusion that it must have something to do with the use of namespaces. What is the solution to this if I wanted to retain the namespaces?

Here is an excerpt of the method definitions. Do I have to dereference the namespace? (I don't think that even means anything haha)

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

解决方案

The friend declaration (non-template non-member) does not match the befriended function templates. I would advice you to provide the implementation inside the class definition:

    template<typename T>
    class Packable
        friend sf::Packet& operator <<(sf::Packet& packet, T &t) {
           return packet << t;
        }
    //...

This allows for a free non-template function that will be generated by the compiler on demand. Other alternatives include befriending the template or the template specialization that you care about.

Of course, you can ignore friendship completely and just provide the templates at namespace level, since they are implemented in terms of a public function…


Related:

这篇关于C ++中的命名空间类模板继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆