开发者

Not sure what is causing my insertion overload mess up

Okay, so in my main, when I try to print out the set using the overloaded << insertion operator I get an error saying, error: no match for 'operator<<' in 'std::cout << person_list' and then it goes on to give me an enormous list of candidates that would actually work I suppose. What's interesting is that the for loop in main will print out the set how I would expect the insertion overload to print out.. I also already have a overload for a person struct already.. Any ideas?

Here's what I have so far:

#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; 
}

Here's the main:

#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;       
}   
}

Here's the last relevant part I guess:

#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


Do you have a forward declaration for the operator<< in your header (personDirectory.h i assume)? i.e:

ostream& operator<<(ostream& out,const set<person*>& s);

If you don't, you need one.

Note: the friend declaration isn't enough afaik.


you have operator overload for set, but operator overload for person is missing!

it will look like this: ostream& operator<<(ostream& out, const person& p) { // TODO...... }

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜