具有纯虚函数的模板类的C ++语法?

时间:2015-11-14 02:03:01

标签: c++ templates virtual pure-virtual

我对C ++模板的熟悉程度很低,而且在模板类中添加纯虚函数已经无法满足编译器的需求。

以下代码......

#include <iostream>
#include <string>
#include <sstream>

class Pearl
{
    public:
        Pearl(int value);
        virtual ~Pearl();

    protected:
        int mValue;
};

Pearl::Pearl(int value)
    : mValue(value)
{
    std::cout << "$" << mValue << " Pearl created" << std::endl;
}

Pearl::~Pearl()
{
    std::cout << "$" << mValue << " Pearl destroyed" << std::endl;
}

///////////////////////////////////////////////////////////////////////

class Oyster
{
    public:
        Oyster(std::string str, int value);
        virtual ~Oyster();

    protected:
        Pearl mPearl;
        std::string mName;

    friend class OStreamer;
};

Oyster::Oyster(std::string name, int value)
    : mName(name)
    , mPearl(value)
{
    std::cout << "Oyster " << mName << " created" << std::endl;
}

Oyster::~Oyster()
{
    std::cout << "Oyster " << mName << " destroyed" << std::endl;
}

///////////////////////////////////////////////////////////////////////

template <typename T> class Streamer
{
    public:
        Streamer(T& rT, unsigned int flags);
        ~Streamer();

        virtual std::ostream Display() = 0;

        static const unsigned int A = 0x1;
        static const unsigned int B = 0x2;

    protected:
        T& mrT; // Aah pity the foo!
        unsigned int mFlags;

    friend class OStreamer;
};

///////////////////////////////////////////////////////////////////////

class OStreamer : public Streamer<Oyster>
{
    public:
        OStreamer(Oyster oyster, unsigned int flags);
        virtual std::ostream Display();
};

OStreamer::OStreamer(Oyster oyster, unsigned int flags)
    : Streamer<Oyster>(oyster, flags)
{
}

std::ostream OStreamer::Display()
{
    std::ostringstream oss;

    oss << "Oyster[" << mrT.mName << "]" << std::endl;
}

///////////////////////////////////////////////////////////////////////

template <typename T> std::ostream& operator<<(std::ostream& os, const Streamer<T> streamer)
{
    return os;
}

///////////////////////////////////////////////////////////////////////

int main()
{
    Oyster sam("Sam", 50);

    std::cout << OStreamer(sam, OStreamer::A) << std::endl;

    return 0;
}

...生成以下编译器错误(使用g ++ 4.4.7):

>g++ main.cpp
main.cpp: In function ‘int main()’:
main.cpp:107: error: cannot allocate an object of abstract type ‘Streamer<Oyster>’
main.cpp:56: note:   because the following virtual functions are pure within ‘Streamer<Oyster>’:
main.cpp:61: note:      std::ostream Streamer<T>::Display() [with T = Oyster]

有人可以帮助我理解为什么编译器不接受OStreamer :: Display()的实现作为Streamer :: Display()的必需覆盖?

作为上下文:这是实验性的,人为的代码,让我熟悉将模板类与纯虚函数相结合。我想用这段代码完成的是为OStreamer对象实现一个调用OStreamer :: Display的otream运算符。我认识到ostream运算符可能需要一些调整,但我认为这不是正在发挥作用的问题。

1 个答案:

答案 0 :(得分:2)

将操作员过载更改为:

template <typename T> std::ostream& operator<<(std::ostream& os,
                           const Streamer<T> &streamer)
{
    return os;
}

传递const Streamer<t>参数需要制作基类的副本,当然这是不可能的,因为它是虚拟的。

传递引用可以避免此问题。