【发布时间】:2014-03-27 01:16:28
【问题描述】:
我正在尝试将元素插入到集合“教师”中。教师是设置的,这是学校结构的一部分。学校结构在另一个叫做城镇的集合内。我正试图将“格林”老师送入“布朗”担任校长的学校。我用find找到学校,但我无法让他进入。操作符
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||在函数'bool transform(const char*, const char*)'中:| /home/ulohaa1/main.cpp|84|错误:没有匹配函数调用‘std::set >::insert(std::string&) const’| /home/michal/Desktop/prog/ulohaa1/main.cpp|84|注意:候选人是:| /usr/include/c++/4.6/bits/stl_set.h|407|注意: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>, _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|注意:从 'const std::set >' 到 'std::set >' 的隐式 'this' 参数没有已知的转换>'| /usr/include/c++/4.6/bits/stl_set.h|444|注意: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 = std: :basic_string]| /usr/include/c++/4.6/bits/stl_set.h|444|注意:候选人需要2个参数,提供1个| /usr/include/c++/4.6/bits/stl_set.h|464|注意:模板 void std::set::insert(_InputIterator, _InputIterator) [with _InputIterator = _InputIterator, _Key = std::basic_string, _Compare = std: :less >, _Alloc = std::allocator >]| ||=== 构建完成:7 个错误,0 个警告 ===|
谢谢你们的帮助
【问题讨论】:
-
C++ 区分大小写,因此 {School *pSchool = new school():} 行不应该编译(更不用说行尾的冒号了。
-
@Andy,除非
school是从School秘密继承的,但从OP 的问题来看,这不太可能 -
@awesomeyi 同意。也许OP可以澄清。
-
你怎么会有一套?仅此一项就无法编译,因为 School 定义的顺序不少于。更好的是,发布一个真实的、可编译的、但很小的程序。匆忙发布代码并让其他人尝试找出所有的拼写错误并没有帮助。
-
/// error到底是什么意思??