2016-05-27 5 views
-1

私は以前に作成した著者をデータベースに追加し、書籍を作成してから著者を本から割り当てることができる書店管理システムを作成しようとしていますデータベース(これはstd :: list)です。 FindAdd関数は、データベース内の著者のリストを反復し、その中の指定されたオブジェクト(一時的な著者)を見つけて、このオブジェクトをブックの著者リストに追加することになっています。stdリスト内のオブジェクトを見つけて別のリストに追加する

イテレータをオブジェクトにキャストしようとしているので、著者を追加できますが、この行はこのプログラムをコンパイルできません(Book :: AddAuthorを呼び出す関数はありません著者*))。私はキャスティングなしでそれを試しましたが、もちろん動作しません。これをどうすれば解決できますか?あるいは、私がここでやろうとしていることを達成するためのより簡単な方法がありますか?

class Author 
{ 
private: 
    string name, lname; 
public: 
    bool operator==(const Author & a) const 
    { 
     bool test=false; 
     if(!(this->name.compare(a.name) && this->lname.compare(a.lname))) 
      test=true; 
     return test; 
    } 
    Author(string namex, string lnamex) 
    { 
     name=namex; 
     lname = lnamex; 
    } 
}; 
class Book 
{ 
public: 
    list <Author> Authorzy; 
    string tytul; 

    void AddAuthor(Author & x) 
    { 
     Authorzy.push_back(x); 
    } 
    Book(string tytulx) 
    { 
     tytul = tytulx; 
    } 
}; 

class Database 
{ 
    protected: 
    list <Author> authors; 
    public: 
    void AddAuthor(Author x) 
    { 
    authors.push_back(x); 
    } 
    list <Author> getAuthors 
    { 
    return authors; 
    } 
}; 

void FindAdd(Author & x, Book &y, Database & db) 
{ 
    list <Author>:: iterator xx; 
     xx = find(db.getAuthors().begin(), db.getAuthors().end(), x); 
     if (xx != db.getAuthors().end()) 
     y.AddAuthor(&*xx); 
     else cout << "Author not found"; 
} 

int main(){ 
Author testauthor("test", "test"); 
Database testdb; 
testdb.AddAuthor(testauthor); 
Book testbook("Mainbook"); 
FindAdd(Author notfound("Another", "Guy"), testbook, testdb); 
FindAdd(testauthor, testbook, testdb); 
} 

答えて

1

AddAuthorあなたは空想何もする必要はいけないので、ちょうどBookリファレンスを取ります。

if (xx != db.getAuthors().end()) { 
    y.AddAuthor(*xx); // Just dereference the iterator and pass it 
         // in, c++ takes care of the rest 
} else { 
    cout << "Author not found"; 
} 
関連する問題