2

「<<」インターフェイスのようなストリームを使用して、簡単なロギング機能をゼロから構築しようとしていますが、コンパイラの問題が発生しています。これが私のコードの基本です:

#include <string.h>
#include <sstream>
#include <iostream>

class Logger
{
public:
    class Buffer
    {
    public:
        Buffer(Logger &parent) : mPar(parent)
        {
        }

        ~Buffer(void)
        {
            mPar.endline(os);
        }

        template<class T>
        Buffer& operator<<(const T& x)
        {
            os << x;
            return *this;
        }

        Logger& mPar;
        std::ostringstream os;
    };

    Buffer write(void)
    {
        return Buffer(*this);
    }

    void endline(std::ostringstream& os)
    {
        std::cout << os.str() << std::endl;
    }
};

int main(int argc, char **argv)
{
    Logger log;

    log.write() << "fred" << 3 << "bob";
}

私が得ているエラーは次のとおりです。

In file included from /usr/include/c++/4.6/ios:45:0,
                 from /usr/include/c++/4.6/istream:40,
                 from /usr/include/c++/4.6/sstream:39,
                 from test.cpp:2:
/usr/include/c++/4.6/bits/ios_base.h: In copy constructor ‘std::basic_ios<char>::basic_ios(const std::basic_ios<char>&)’:
/usr/include/c++/4.6/bits/ios_base.h:788:5: error: ‘std::ios_base::ios_base(const std::ios_base&)’ is private
/usr/include/c++/4.6/bits/basic_ios.h:64:11: error: within this context
In file included from test.cpp:2:0:
/usr/include/c++/4.6/sstream: In copy constructor ‘std::basic_ostringstream<char>::basic_ostringstream(const std::basic_ostringstream<char>&)’:
/usr/include/c++/4.6/sstream:373:11: note: synthesized method ‘std::basic_ios<char>::basic_ios(const std::basic_ios<char>&)’ first required here
/usr/include/c++/4.6/streambuf: In copy constructor ‘std::basic_stringbuf<char>::basic_stringbuf(const std::basic_stringbuf<char>&)’:
/usr/include/c++/4.6/streambuf:782:7: error: ‘std::basic_streambuf<_CharT, _Traits>::basic_streambuf(const __streambuf_type&) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_streambuf<_CharT, _Traits>::__streambuf_type = std::basic_streambuf<char>]’ is private
/usr/include/c++/4.6/sstream:60:11: error: within this context
/usr/include/c++/4.6/sstream: In copy constructor ‘std::basic_ostringstream<char>::basic_ostringstream(const std::basic_ostringstream<char>&)’:
/usr/include/c++/4.6/sstream:373:11: note: synthesized method ‘std::basic_stringbuf<char>::basic_stringbuf(const std::basic_stringbuf<char>&)’ first required here
test.cpp: In copy constructor ‘Logger::Buffer::Buffer(const Logger::Buffer&)’:
test.cpp:8:8: note: synthesized method ‘std::basic_ostringstream<char>::basic_ostringstream(const std::basic_ostringstream<char>&)’ first required here
test.cpp: In member function ‘Logger::Buffer Logger::write()’:
test.cpp:33:22: note: synthesized method ‘Logger::Buffer::Buffer(const Logger::Buffer&)’ first required here

これまでに見つけたものから、エラーは ostringstream でコピー コンストラクターを呼び出すことができないためです。私が知る限り、私はコピー コンストラクターを直接呼び出していません。また、Buffer をコピーしておらず、'return' ステートメントで構築しているだけです。

もう 1 つの興味深い点は、このコードは、アプリケーション (GCC 4.6.3 を使用してコンパイルされている) に統合する前にコードをノックアップした Visual Studio 2010 で正常にコンパイルされることです。

問題を正しく解釈しましたか? もしそうなら、暗黙のコピーはどこにあり、どうすればそれを排除できますか?

4

1 に答える 1