2016-10-19 2 views
0

Это сводит меня с ума ... У меня есть заголовок ах со следующим содержимымошибка: не подходит для «оператора <<»

#if !defined(PRETTY_PRINT_H) 
#define PRETTY_PRINT_H 

#include <iostream> 
#include <vector> 

std::ostream& operator<<(std::ostream& os, const std::vector<int> v); 

#endif 

источник b.cc с

... 
#include "b.h" 
#include "a.h" 
... 
void 
ns::ClD::myfunc(args...) { 
    const std::vector<int> test(2,1); 
    ... 
    std::cout << "test : " << test << std::endl; 
    ... 
} 
... 

и заголовок ЬН

... 
namespace ns { 
    class ClD: public ClB 
    private: 
     virtual void 
     myfunc(args...); 
    ... 

При составлении b.cc, я получаю

b.cc: In member function ‘virtual void myfunc()’: 
b.cc:134:26: error: no match for ‘operator<<’ (operand types are ‘std::basic_ostream<char>’ and ‘const std::vector<int>’) 
    std::cout << "test : " << test << std::endl; 
         ^
b.cc:134:26: note: candidates are: 
... 

UPDATE

Были кандидаты, поступающие из моего заголовка (номер строки не совпадают, так как я удалил несколько комментировали и пустые строки в этом OP).

В тех случаях, когда вопрос с аргументом 1, например,

In file included from /usr/include/c++/4.8/iostream:39:0, 
       from a.h:12, 
       from b.cc:13: 
/usr/include/c++/4.8/ostream:108:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>::__ostream_type& (*)(std::basic_ostream<_CharT, _Traits>::__ostream_type&)) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>] 
     operator<<(__ostream_type& (*__pf)(__ostream_type&)) 
    ^
/usr/include/c++/4.8/ostream:108:7: note: no known conversion for argument 1 from ‘const std::vector<int>’ to ‘std::basic_ostream<char>::__ostream_type& (*)(std::basic_ostream<char>::__ostream_type&) {aka std::basic_ostream<char>& (*)(std::basic_ostream<char>&)}’ 
/usr/include/c++/4.8/ostream:117:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>::__ios_type& (*)(std::basic_ostream<_CharT, _Traits>::__ios_type&)) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>; std::basic_ostream<_CharT, _Traits>::__ios_type = std::basic_ios<char>] 
     operator<<(__ios_type& (*__pf)(__ios_type&)) 
    ^
/usr/include/c++/4.8/ostream:117:7: note: no known conversion for argument 1 from ‘const std::vector<int>’ to ‘std::basic_ostream<char>::__ios_type& (*)(std::basic_ostream<char>::__ios_type&) {aka std::basic_ios<char>& (*)(std::basic_ios<char>&)}’ 
... 

В тех случаях, когда вопрос с аргументом 2,

In file included from /usr/include/c++/4.8/iostream:39:0, 
       from a.h:12, 
       from b.cc:13: 
/usr/include/c++/4.8/ostream:548:5: note: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, const unsigned char*) 
    operator<<(basic_ostream<char, _Traits>& __out, const unsigned char* __s) 
    ^
/usr/include/c++/4.8/ostream:548:5: note: template argument deduction/substitution failed: 
b.cc:135:29: note: cannot convert ‘test’ (type ‘const std::vector<int>’) to type ‘const unsigned char*’ 
    std::cout << "test : " << test << std::endl; 
          ^
In file included from /usr/include/c++/4.8/iostream:39:0, 
       from a.h:12, 
       from b.cc:13: 
/usr/include/c++/4.8/ostream:543:5: note: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, const signed char*) 
    operator<<(basic_ostream<char, _Traits>& __out, const signed char* __s) 
    ^
/usr/include/c++/4.8/ostream:543:5: note: template argument deduction/substitution failed: 
b.cc:135:29: note: cannot convert ‘test’ (type ‘const std::vector<int>’) to type ‘const signed char*’ 
    std::cout << "test : " << test << std::endl; 
          ^
... 

Но я не уверен, что если какой-либо из кандидаты, проанализированные в сообщенных сообщениях, действительно поступают из моего прототипа или из других включенных стандартных заголовков.

Любые подсказки о том, в чем проблема?

+2

Является ли ваш оператор признанным в пространстве имен, который не является глобальным? – krzaq

+3

При уменьшении до [голого минимума] (http://ideone.com/n3Xg1n) он работает. Так что независимо от проблемы, ваши фрагменты кода не демонстрируют это. – StoryTeller

+1

Ошибка, вероятно, в одном из ваших «...» Когда вы не знаете, в чем проблема, если предположить, что проблема не проблема, это часто проблема. Код компилируется: https://godbolt.org/g/AKHBPK – xaxxon

ответ

0

Благодаря всем комментариям, которые направляли меня на дополнительные части, необходимые для обновления OP, и для проверки поведения моего кода я нашел виновника.

Была другая перегрузка, которая мешала. В «...» с, там был включен заголовок, который содержал

namespace ns { 
    class ClB2 
    { 
    ... 
    }; 

    inline 
    std::ostream& operator<<(std::ostream& os, const ClB2& me) { 
    (Irrelevant code; when commented still produced the error) 
    return os; 
    } 
} 

Когда я переехал перегрузки вне пространства имен, так как

inline 
std::ostream& operator<<(std::ostream& os, const ns::ClB2& me) { 
    (Irrelevant code) 
    return os; 
} 

он работал. Тем не менее, я не уверен, почему это привело к ошибке.

Смежные вопросы