好的,所以在我的主要内容中,当我尝试使用重载<<<<<<<<<<<插入操作符我收到错误说,错误:'operator<<'不匹配在'std :: cout<< person_list'然后它继续给我一个巨大的候选人名单,我认为这些候选人实际上可行。有趣的是,main中的for循环将打印出我将期望插入重载打印出来的集合。我已经有一个人类结构的重载已经..任何想法?
这是我到目前为止所拥有的:
#include <iostream>
#include <string>
#include <iterator>
#include <algorithm>
#include "personDirectory.h"
using namespace std;
void person_directory::addPerson(const person& p){
people.push_back(p);
idMap.insert(pair<int,person*>(p.id, const_cast<person*>(&p)));
phoneMap.insert(pair<int,person*>(p.phoneNumber, const_cast<person*>(&p)));
ageMap.insert(pair<int,person*>(p.age, const_cast<person*>(&p)));
nameMap.insert(pair<string,person*>(p.name, const_cast<person*>(&p)));
cout << "*******************" << endl << endl;
cout << "Person: " << endl;
cout << p;
cout << "was added" << endl;
cout << "*******************" << endl;
}
set<person*> person_directory::findByName(string fn){
//maybe construct a set using the constructor with
//iterator from beginning of map to end of map
set<person*> temp;
for(multimap<string, person*>::const_iterator it = nameMap.begin();
it != nameMap.end();){
if(it->first == fn)
temp.insert(it->second);
it++;
}
return temp;
}
ostream& operator<<(ostream& out,const set<person*>& s){
out << "Your set: " << endl;
for(set<person*>::const_iterator it = s.begin(); it != s.end(); ++it){
out << **it;
}
return out;
}
这是主要的:
#include <iostream>
#include <vector>
#include "person.h"
#include "personDirectory.h"
using namespace std;
int main(){
//testing person
person* p1 = new person(1, "Carl Jr", 36, 4563456);
person* p2 = new person(2, "Indiana Jones", 24, 6782345);
person* p3 = new person(3, "Bobby Boy", 23, 6782275);
person* p4 = new person(4, "Robby", 19, 6782234);
person* p5 = new person(5, "Carl Sagan", 47, 4562234);
person* p6 = new person(6, "Bobby Boy", 11, 6786657);
person_directory* person_dir = new person_directory();
person_dir->addPerson(*p1);
person_dir->addPerson(*p2);
person_dir->addPerson(*p3);
person_dir->addPerson(*p4);
person_dir->addPerson(*p5);
person_dir->addPerson(*p6);
set<person*> person_list;
person_list = person_dir->findByName("Bobby Boy");
cout << person_list;
for(set<person*>::const_iterator it = person_list.begin(); it != person_list.end(); ++it){
cout << **it;
}
}
这是我猜的最后一个相关部分:
#ifndef _PERSON_DIRECTORY_H_
#define _PERSON_DIRECTORY_H_
#include "person.h"
#include <map>
#include <set>
#include <vector>
using std::map;
using std::set;
using std::vector;
using std::multimap;
class person_directory {
private:
// this vector will contain the actual data
vector<person> people;
// this maps serve as indexes; they contain pointers to the data in people
map<int,person*> idMap;
multimap<int,person*> phoneMap, ageMap;
multimap<string,person*> nameMap;
public:
void addPerson(const person& p);
set<person*> findByName(string fn);
person* findById(int id);
set<person*> findByAge(int age);
set<person*> findByPhone(int p);
friend std::ostream& operator<<(std::ostream& out, const std::set<person*>& s);
};
#endif
答案 0 :(得分:0)
您是否有运营商的转发声明&lt;&lt;在你的标题中(personDirectory.h我假设)?即:
ostream& operator<<(ostream& out,const set<person*>& s);
如果你没有,你需要一个。
注意:朋友声明不够。
答案 1 :(得分:0)
你有设置的运算符重载,但缺少运算符重载的人!
它看起来像这样: ostream的和放;运算符&lt;&lt;(ostream&amp; out,const person&amp; p) { // 去做...... }