我試圖插入元素設置「教師」。教師是結構,學校的一部分。學校結構在另一個叫做城鎮的地方。我試圖把老師「綠色」放到學校裏,「布朗」是校長。我用find來找到學校,但我不能讓他進來。運營商<,==被定義爲比較校長。通過迭代器插入元素C++
bool operator<(const School& l, const School& r){
return (l.headmaster) < (r.headmaster);
}
bool operator==(const School& l, const School& r){
return (l.headmaster) == (r.headmaster);
}
struct School {
string headmaster;
set <string> teachers;
};
set<School>::iterator it;
set <School> town;
// now I alocated few schools and insert them into town,
School *pSchool = new School(): // i will use pSchool to find school with brown as headmaster
pSchool > headmaster = "Brown"; //
it = rozvrh.find(*pSchool);
cout << it->headmaster // gives Brown
it->teachers.insert("Green"); /// error
編輯..錯誤
|| === ulohaa1,調試=== | /home/ulohaa1/main.cpp||在函數'布爾變換(常量字符*,常量字符*)':| /home/ulohaa1/main.cpp|84|錯誤:沒有匹配函數調用'std :: set> :: insert(std :: string &)const'| /home/michal/Desktop/prog/ulohaa1/main.cpp|84|note:考生是:| /usr/include/c++/4.6/bits/stl_set.h|407|note:std :: pair,_Compare,typename _Alloc :: rebind < _Key> :: other> :: const_iterator,bool> std :: set < _Key,_Compare,_Alloc> :: insert(const value_type &)[with _Key = std :: basic_string,_Compare = std :: less>,_Alloc = std :: allocator>,typename std :: _ Rb_tree < _Key,_Key,std :: _ Identity < _Key>,_Compare,typename _Alloc :: rebind < _Key> :: other> :: const_iterator = std :: _ Rb_tree_const_iterator>,std :: set < _Key,_Compare,_Alloc> :: value_type = std :: basic_string ] | /usr/include/c++/4.6/bits/stl_set.h|407|note:對於隱式'this'參數從'const std :: set>'到'std :: set>'|沒有已知的轉換。 /usr/include/c++/4.6/bits/stl_set.h|444|note:std :: set < _Key,_Compare,_Alloc> :: iterator std :: set < _Key,_Compare,_Alloc> :: insert(std :: set < _Key,_Compare,_Alloc> :: const_iterator,const value_type )[with _Key = std :: basic_string,_Compare = std :: less>,_Alloc = std :: allocator>,std :: set < _Key, _Compare,_Alloc> :: iterator = std :: _ Rb_tree_const_iterator>,std :: set < _Key,_Compare,_Alloc> :: const_iterator = std :: _ Rb_tree_const_iterator>,std :: set < _Key,_Compare,_Alloc> :: value_type =性病:: basic_string的] | /usr/include/c++/4.6/bits/stl_set.h|444|note:候選人需要2個參數,1個提供| /usr/include/c++/4.6/bits/stl_set.h|464|note:template void std :: set :: insert(_InputIterator,_InputIterator)[with _InputIterator = _InputIterator,_Key = std :: basic_string,_Compare = std :: less>,_Alloc = std :: allocator>] | || ===構建完成:7個錯誤,0個警告=== |
THX您的幫助球員
C++是大小寫敏感的,因此線{學校* pSchool =新學校():}不應該編譯(更不要說冒號在行尾) – Andy
@安迪,除非'學校'是祕密地繼承'學校',但從OP的問題來看,這不太可能 – yizzlez
@awesomeyi同意。 OP可以澄清, – Andy