×

Loading...
Ad by
  • 最优利率和cashback可以申请特批,好信用好收入offer更好。请点链接扫码加微信咨询,Scotiabank -- Nick Zhang 6478812600。
Ad by
  • 最优利率和cashback可以申请特批,好信用好收入offer更好。请点链接扫码加微信咨询,Scotiabank -- Nick Zhang 6478812600。

make the C++ code compiled

本文发表在 rolia.net 枫下论坛#include <string>
using std::string;
#include <map>
using std::map;

class A {
public:
A(const string &s) : _s(s) { }

bool operator<(const A& a) const { return _s < a._s; }

string _s;
};

int main()
{
map<A, int> m;
char* s = "abc";
A foo(string(s));
map<A, int>::iterator iter = m.find(foo);
}
---

I'm using gcc version 3.2.2 and this is the error message:

---

wtf.cpp: In function `int main()':
wtf.cpp:20: no matching function for call to `std::map<A, int, std::less,
std::allocator<std::pair<const A, int> > >::find(A
(&)(std::basic_string<char, std::char_traits<char>,
std::allocator<char> >))
'
/usr/include/c++/3.2.2/bits/stl_map.h:332: candidates are:
std::_Rb_tree<_Key,
std::pair<const _Key, _Tp>, std::_Select1st<std::pair<const _Key,
_Tp> >,
_Compare, _Alloc>::iterator std::map<_Key, _Tp, _Compare,
_Alloc>::find(const _Key&) [with _Key = A, _Tp = int, _Compare =
std::less, _Alloc = std::allocator<std::pair<const A, int> >]
/usr/include/c++/3.2.2/bits/stl_map.h:345:
std::_Rb_tree<_Key,
std::pair<const _Key, _Tp>, std::_Select1st<std::pair<const _Key,
_Tp> >,
_Compare, _Alloc>::const_iterator std::map<_Key, _Tp, _Compare,
_Alloc>::find(const _Key&) const [with _Key = A, _Tp = int, _Compare =
std::less, _Alloc = std::allocator<std::pair<const A, int> >]更多精彩文章及讨论,请光临枫下论坛 rolia.net
Report

Replies, comments and Discussions:

  • 工作学习 / IT技术讨论 / make the C++ code compiled
    本文发表在 rolia.net 枫下论坛#include <string>
    using std::string;
    #include <map>
    using std::map;

    class A {
    public:
    A(const string &s) : _s(s) { }

    bool operator<(const A& a) const { return _s < a._s; }

    string _s;
    };

    int main()
    {
    map<A, int> m;
    char* s = "abc";
    A foo(string(s));
    map<A, int>::iterator iter = m.find(foo);
    }
    ---

    I'm using gcc version 3.2.2 and this is the error message:

    ---

    wtf.cpp: In function `int main()':
    wtf.cpp:20: no matching function for call to `std::map<A, int, std::less,
    std::allocator<std::pair<const A, int> > >::find(A
    (&)(std::basic_string<char, std::char_traits<char>,
    std::allocator<char> >))
    '
    /usr/include/c++/3.2.2/bits/stl_map.h:332: candidates are:
    std::_Rb_tree<_Key,
    std::pair<const _Key, _Tp>, std::_Select1st<std::pair<const _Key,
    _Tp> >,
    _Compare, _Alloc>::iterator std::map<_Key, _Tp, _Compare,
    _Alloc>::find(const _Key&) [with _Key = A, _Tp = int, _Compare =
    std::less, _Alloc = std::allocator<std::pair<const A, int> >]
    /usr/include/c++/3.2.2/bits/stl_map.h:345:
    std::_Rb_tree<_Key,
    std::pair<const _Key, _Tp>, std::_Select1st<std::pair<const _Key,
    _Tp> >,
    _Compare, _Alloc>::const_iterator std::map<_Key, _Tp, _Compare,
    _Alloc>::find(const _Key&) const [with _Key = A, _Tp = int, _Compare =
    std::less, _Alloc = std::allocator<std::pair<const A, int> >]更多精彩文章及讨论,请光临枫下论坛 rolia.net
    • Try this out, be sure to use g++ instead of gcc, because gcc can't link some stl lib. also don't try to use: char* s = "abc"; A foo(string(s)); that's not standard C++ style
      #include <string>
      using std::string;
      #include <map>
      using std::map;

      class A {
      public:
      A(const string &s) : _s(s) { }

      bool operator<(const A& a) const { return _s < a._s; }
      //
      string _s;
      };

      int main()
      {
      map<A, int> m;

      string s = "abc";
      A foo(s);
      map<A, int>::iterator iter = m.find(foo);
      }