1

以下のマップで << 演算子がオーバーロードされている場合でも、非常に奇妙なエラーが発生します。Feature.h は Session.C という名前のクラスで使用され、<< を使用してマップ tKeyboard の値を出力しようとしています。私はこれらすべてに不慣れで、これは非常に古いコードであるため、この問題について困惑しています。何か提案できますか。感謝します。

Feature.h

class Feature {
..
public:

 typedef map<string,KeyStroke,std::less<string>,std::allocator<std::pair<const string, KeyStroke> > > tKeyboard;
                               // It is a map of keys
                               // key is the name of the keystroke
      typedef  tKeyboard::const_iterator cKeysIter;
      typedef  tKeyboard::iterator KeysIter;
..
  private:
  friend ostream &operator<<(ostream &os,const tKeyboard &);
..
};

Feature.C

ostream &operator<<(ostream &os,const Feature::tKeyboard &k)
{
    os <<" Keyboard >\n";
    for (Feature::cKeysIter i=k.begin() ; i!= k.end() ; i++)
    {
        os << "key name:"<<(*i).first << " ="<< (*i).second ;
    }
    return os;
}

セッション.C

 if (keyStrokeVal.empty()){
       Traced(3)<<"sendKey:Error, key "<<keyname<<" not defined or empty\n";
       Traced(7)<<"Printing Feature "<<mCurrentFeature->get_mName() <<" 's keyboard \n" << mCurrentFeature->get_mKeyboard();
       Traced(7)<<"\n";
       return(-1);
    }

エラー:

Session.C:541: error: no match for âoperator<<â in âstd::operator<< [with _Traits = std::char_traits<char>](((std::basic_ostream<char, std::char_traits<char> >&)((std::basic_ostream<char, std::char_traits<char> >*)std::operator<< [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>](((std::basic_ostream<char, std::char_traits<char> >&)((std::basic_ostream<char, std::char_traits<char> >*)std::operator<< [with _Traits = std::char_traits<char>](((std::basic_ostream<char, std::char_traits<char> >&)((std::basic_ostream<char, std::char_traits<char> >*)((std::basic_ostream<char, std::char_traits<char> >*)std::operator<< [with _Traits = std::char_traits<char>](((std::basic_ostream<char, std::char_traits<char> >&)((std::basic_ostream<char, std::char_traits<char> >*)((std::basic_ostream<char, std::char_traits<char> >*)std::operator<< [with _Traits = std::char_traits<char>](((std::basic_ostream<char, std::char_traits<char> >&)((std::basic_ostream<char, std::char_traits<char> >*)std::operator<< [with _Traits = std::char_traits<char>](((std::basic_ostream<char, std::char_traits<char> >&)((std::basic_ostream<char, std::char_traits<char> >*)((std::ostream*)LogMgr::traceRovr())->std::basic_ostream<_CharT, _Traits>::operator<< [with _CharT = char, _Traits = std::char_traits<char>](std::endl [with _CharT = char, _Traits = std::char_traits<char>]))), ((const char*)"Session.C")))), ((const char*)" ")))->std::basic_ostream<_CharT, _Traits>::operator<< [with _CharT = char, _Traits = std::char_traits<char>](541))), ((const char*)" - ")))->std::basic_ostream<_CharT, _Traits>::operator<< [with _CharT = char, _Traits = std::char_traits<char>](out_time))), ((const char*)"Printing Feature ")))), ((const std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)((const std::string*)((Session*)this)->Session::mCurrentFeature->Feature::get_mName()))))), ((const char*)" \'s keyboard \012")) << ((Session*)this)->Session::mCurrentFeature->Feature::get_mKeyboard()â
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/ostream:108: note: candidates are: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>& (*)(std::basic_ostream<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/ostream:117: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ios<_CharT, _Traits>& (*)(std::basic_ios<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/ostream:127: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::ios_base& (*)(std::ios_base&)) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/ostream:165: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/ostream:169: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long unsigned int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/ostream:173: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(bool) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/ostream.tcc:91: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(short int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/ostream:180: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(short unsigned int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/ostream.tcc:105: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/ostream:191: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(unsigned int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/ostream:200: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long long int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/ostream:204: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long long unsigned int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/ostream:209: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(double) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/ostream:213: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(float) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/ostream:221: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long double) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/ostream:225: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(const void*) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/ostream.tcc:119: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_streambuf<_CharT, _Traits>*) [with _CharT = char, _Traits = std::char_traits<char>]
4

3 に答える 3

1

問題は、friend宣言が最初に関数を宣言する (しかし定義しない) 場合、その関数の名前が周囲のスコープに導入されないことです。C++11 の引用 [namespace.memdef]§3:

非ローカル クラスの宣言が最初にクラスまたは関数を宣言する場合friend、フレンド クラスまたは関数は、最も内側の囲み名前空間のメンバーです。フレンドの名前は、非修飾ルックアップ (3.4.1) または修飾ルックアップ (3.4.3) によって、その名前空間スコープで一致する宣言が提供されるまで(クラス定義がフレンドシップを付与する前または後に) 検出されません。フレンド関数が呼び出された場合、その名前は、関数の引数の型に関連付けられた名前空間とクラスからの関数を考慮する名前検索によって検出される場合があります (3.4.2)。

(私のものを強調)

したがって、解決策は別のクラス外宣言です。

std::ostream &operator<<(std::ostream &os,const Feature::tKeyboard &);
于 2013-09-12T07:58:40.970 に答える
0

考えられる理由は、グローバル名前空間が検索されていないために your::operator<<が見つからないことです。operator<<に追加することはできません::std::。この場合は、ローカルで修正できますusing ::operator<<

于 2013-09-12T07:39:05.717 に答える
-2

ADL を支援するために出力演算子を名前空間 std に追加しようとしましたか?

namespace std{
ostream &operator<<(ostream &os,const Feature::tKeyboard &k)
{
  os <<" Keyboard >\n";
  for (Feature::cKeysIter i=k.begin() ; i!= k.end() ; i++)
  {
    os << "key name:"<<(*i).first << " ="<< (*i).second ;
  }
  return os;
}
}
于 2013-09-12T07:12:10.260 に答える