错误:不匹配'operator<<'

时间:2016-10-19 09:35:29

标签: c++ operator-overloading

这让我疯狂...... 我有一个带有以下内容的标题a.h

#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;
  ...
}
...

和标题b.h

...
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:
...

更新

我的标题中有候选人(行号不匹配,因为我在此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;
                             ^
...

但我不确定报告的消息中分析的任何候选人是否实际上来自我的原型或来自其他标准标题的其他人。

有关问题的任何提示?

1 个答案:

答案 0 :(得分: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;
}

它奏效了。 不过,我不确定为什么会产生错误。