亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区
Chinaunix
標(biāo)題:
【SOLVED】<map>容器內(nèi)自定義類型重載<<操作符失敗
[打印本頁(yè)]
作者:
qqqqblog
時(shí)間:
2013-12-27 10:17
標(biāo)題:
【SOLVED】<map>容器內(nèi)自定義類型重載<<操作符失敗
本帖最后由 qqqqblog 于 2013-12-27 15:34 編輯
求助,注釋掉的那行編譯失敗,說(shuō)找不到構(gòu)造函數(shù)
#include <iostream>
#include <string>
#include <map>
using namespace std;
class c
{
string s;
public:
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ù)制代碼
作者:
Herowinter
時(shí)間:
2013-12-27 10:37
本帖最后由 Herowinter 于 2013-12-27 10:38 編輯
回復(fù)
1#
qqqqblog
foo["a"]改為foo['a']
再寫(xiě)一個(gè)空的無(wú)參構(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ù)制代碼
作者:
qqqqblog
時(shí)間:
2013-12-27 10:51
本帖最后由 qqqqblog 于 2013-12-27 11:05 編輯
. . . . . . .
作者:
qqqqblog
時(shí)間:
2013-12-27 11:04
回復(fù)
2#
Herowinter
感謝大師!
請(qǐng)問(wèn)<map>[]操作返回自定義值的引用,為啥要加空構(gòu)造函數(shù)呢?
作者:
wenlq
時(shí)間:
2013-12-27 11:12
stl_map.h
里有
__i = insert(__i, value_type(__k, mapped_type()));
作者:
Herowinter
時(shí)間:
2013-12-27 11:36
回復(fù)
4#
qqqqblog
insert時(shí)會(huì)調(diào)用c類的無(wú)參構(gòu)造函數(shù)
歡迎光臨 Chinaunix (http://www.72891.cn/)
Powered by Discuz! X3.2