0

さて、私のメインでは、オーバーロードされた << 挿入演算子を使用してセットを出力しようとすると、次のエラーが表示されます。続けて、実際に機能すると思われる候補の膨大なリストが表示されます。興味深いのは、メインの for ループが、挿入オーバーロードが出力することを期待する方法でセットを出力することです..私はすでに person 構造体のオーバーロードを既に持っています..

これが私がこれまでに持っているものです:

#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
4

2 に答える 2

0

set の演算子オーバーロードがありますが、person の演算子オーバーロードがありません!

次のようになります: ostream& operator<<(ostream& out, const person& p) { // TODO...... }

于 2011-06-22T05:52:53.477 に答える
0

ヘッダーに operator<< の前方宣言がありますか (personDirectory.hi と仮定します)? すなわち:

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

そうでない場合は、1 つ必要です。

注: フレンド宣言だけでは十分ではありません。

于 2011-06-22T05:47:26.970 に答える