0

std::vector<T>any のコンテンツを出力ストリームに追加できるようにしたいと考えています。私はこのコードを見つけました:

#ifndef DEBUG_H_
#define DEBUG_H_

#include <vector>

template < class T >
std::ostream& operator << (std::ostream& os, const std::vector<T>& v)
{
    os << "[";
    for (typename std::vector<T>::const_iterator ii = v.begin(); ii != v.end(); ++ii)
    {
        os << " " << *ii;
    }
    os << "]";
    return os;
}


#endif /* DEBUG_H_ */

ヘッダーに入れDebug.hます。プロジェクト全体でこの演算子を使用するにはどうすればよいですか?

編集:これが単体テストで機能することを確認しました:

#include "Debug.h"

TEST_F(AuxGTest, testVectorDebug) {
    std::vector<int> vec(10, 42);
    std::cout << "vec: " << vec << std::endl;
}

ただし、log4cxx のログ ステートメントで使用すると機能しません。

#include <log4cxx>
#include "Debug.h"

namespace Foo {
    class Bar { 
        void foo() {
            std::vector<int> vec(10, 42);
            DEBUG("vec: " << vec);
        }

    }

}

これにより、次のコンパイラ メッセージが表示されます。

/usr/local/Cellar/log4cxx/0.10.0/include/log4cxx/helpers/messagebuffer.h:190:47: error: cannot bind 'std::basic_ostream<char>' lvalue to 'std::basic_ostream<char>&&'
4

2 に答える 2

0

log4cxx を使用したユーザー定義演算子についても同様の質問があります。オペレーターを名前空間に配置することを推奨することで、その質問に答えました。log4cxx::helpers見る 。

于 2014-01-10T05:39:48.550 に答える