- 論壇徽章:
- 780
|
本帖最后由 Herowinter 于 2013-12-27 10:38 編輯
回復(fù) 1# qqqqblog
foo["a"]改為foo['a']
再寫一個(gè)空的無參構(gòu)造函數(shù),
這樣可以了。- #include <iostream>
- #include <string>
- #include <map>
- using namespace std;
- class c
- {
- string s;
- public:
- c(){}
- c(string s)
- :s(s)
- {}
- friend ostream& operator <<(ostream& os, const c& c)
- {
- os << c.s;
- return os;
- }
- string get_s()
- {
- return s;
- }
- };
- typedef std::map<char, c> mymap;
- int main ()
- {
- std::string s0 ("Initial string");
- c m("ddd");
- std::cout << m;
- mymap foo;
- c my_c("abcd");
- foo.insert( make_pair<char, c>('a', my_c));
- std::cout << foo['a']; // HELP: Compile error here
- return 0;
- }
復(fù)制代碼 |
|