2016-11-20 1 views
0

私はC++のクラスを勉強しています。私のコースでは、 "book"、 "author"、 "Store"クラスを含むブックストアを作成する必要があります。C++クラス間でオブジェクトを渡す

Bookクラス内で著者の名前を抽出する際に、クラス間でやりとりする方法がわかりません。

私はXcodeでコードの下の部分を実行すると、私は、ルーヴルのクラスに次のエラーの通知を受けていますコンストラクタ:予想 『(』または 『{』

UDPATE 22 - 11月 - 2016 私は私はこれまでやっていることで、コードを更新しました。

は、まず私がBook_copyクラスから作成者の名前を表示しようとすると、私は問題を持っている。私は単にどのように行うのか分からない。

I author.getName()を今のところ入れておきますが、うまくいきません。

2番目の問題は、Disable AuthorコピーコンストラクタとBookコンストラクタに付属するエラーです。 ???

第3の問題点は、上記が修正されていると仮定すると、このプログラムを作成するために私が受け取った指示に完全に従わないということです。

Bookクラスは、によって特徴づけする必要があります。文字列のタイトル、作者への一定の基準、そしてそれがで書かれていた言語のための別の文字列私は最初の場所でねじ込み持って

を使用しようと作者へのconst参照...

#include <iostream> 
#include <vector> 
#include <string> 

using namespace std; 


class Author{ 
private: 
    string name; 
    bool award; 

public: 
    Author(string name, bool award=false) : name(name), award(award) {} 
    const string getName(){ return name; } 
    const bool getAward() { return award; } 
    Author(Author const&) = delete; 
}; 

class Book{ 
private: 
    string title; 
    Author &author; 
    string language; 

public: 
    Book(string title, Author author, string language) : title(title), author(author), language(language) {} 
    const string getTitle() { return title; } 
    string getAuthor() { return author.getName(); } 
    const string getLanguage() { return language; } 
    void display_book() { cout << title << author.getName() << endl; } 
    ~Book() { cout << title << author.getName() << endl;} 
    Book(Book const&) = delete; 
}; 

class Book_copy{ 
private: 
    Book &book; 

public: 
    Book_copy(Book &nuovo) : book(nuovo) { 
     cout << book.getTitle() << author.getName() << endl; 
    } 
    /*** Copy constructor ***/ 
    // Book_copy(Book_copy const& dupli) 

    ~Book_copy() { 
     cout << book.getTitle() << author.getName() << endl; 
    } 

    const Book &getBook() const { return book; } 
    void display_book() { cout << book.getTitle() << author.getName() << endl; } 
}; 

class Library{ 
private: 
    vector <Book_copy*> bibil; 
    string name; 

public: 
    Library(string name) : name(name){ 
     cout << name << " is open" << endl; 
    } 
    const string &getName() { return name; } 

    void store(Book_copy const &livre, unsigned int qty = 1) { 
     for(unsigned int i(0); i < qty; i++){ 
      // This is where I need to store the number of book copy in the library... 
     } 
    } 


    ~Library() { 
     cout << name << " is closing down" << endl; 
    } 
}; 


int main() 
{ 
    Author a1("Victor Hugo"), 
    a2("Stephen King"), 
    a3("Raymond Queneau", true); 


    Book o1("Les Misérables", a1, "french"); 
    Book o2("Carrie", a2, "english"); 

    Library biblio("whatever"); 
    biblio.store(o1); 
    biblio.store(o2, 2); 

    return 0; 
} 

はおそらく、私は完全には理解していないいくつかの概念があり、私はそれらの問題を作成しています理由です。

私に教えてください。

UPDATE 29 - 11月 - 2016

誰?私はここに円を描いています。私が働くことを得るたびに、もう何かがもう働きます... :( ビビリエテクベクトルが空であることを除いて、以下のコードはほとんど動作します。 いくつかの行があり、いくつかは全く役に立たない、私は行く前後にそれを動作させるためにさまざまな事をしようと...

#include <iostream> 
    #include <vector> 
    #include <string> 
    using namespace std; 

    class Auteur{ 
    private: 
     string nom; 
     bool prix; 

    public: 
     Auteur(string nom="", bool prix=false) 
     : nom(nom), prix(prix) {} 

     string getNom() { return nom; } 
     bool getPrix() { return prix; } 
     Auteur(Auteur const&) = delete; 
    }; 

    class Oeuvre{ 
    private: 
     string titre; 
     Auteur &auteur; 
     string langue; 

    public: 
     Oeuvre(string titre, Auteur &auteur, string langue) 
     : titre(titre), auteur(auteur), langue(langue) {} 

     const string getTitre() { return titre; } 
     string getAuteur() const { return auteur.getNom(); } 
     const Auteur &getAut() const { return auteur; } 
     string getLangue() { return langue; } 
     void affiche() const { cout<<"Affiche "<<titre<<auteur.getNom()<<langue << endl; } 
     ~Oeuvre() { cout<<"Destroyed "<<titre<<auteur.getNom()<<langue<< endl; } 
     Oeuvre(Oeuvre const&) = delete; 
    }; 

    class Examplaire{ 
    private: 
     Oeuvre &oeuvre; 

    public: 
     Examplaire(Oeuvre &nuovo) 
     : oeuvre(nuovo){ cout<<"New "<<oeuvre.getTitre()<<oeuvre.getAuteur()<<oeuvre.getLangue() << endl; } 

     Examplaire(Examplaire const& copie) 
     : oeuvre(copie.oeuvre){ cout<<"Copy "<<oeuvre.getTitre()<<oeuvre.getAuteur()<<oeuvre.getLangue() << endl; } 

     const Oeuvre &getOeuvre() const { return oeuvre; } 
     string getTitre() { return //TODO need to return title 
      oeuvre.getTitre(); 
     } 
     string getLangue() { return ""; 
    //  oeuvre.getLangue(); // crashes!! TODO need to return language 
     } 

     void affiche() { cout<<"Affiche "<< oeuvre.getTitre()<<oeuvre.getAuteur()<<oeuvre.getLangue(); 
     } 
    }; 

    class Bibliotheque{ 
    private: 
     vector <Examplaire*> bibil; 
     string name; 

    public: 
     Bibliotheque(string name) : name(name){ 
      cout << "La bibliothèque " << name << " est ouverte !" << endl; 
     } 

     const string &getNom() { return name; } 

     void stocker(Oeuvre & livre, unsigned int qty = 1) { 
      for(int i(0); i < qty; i++){ 
       Examplaire temp = livre; 
       bibil.push_back(&temp); 
      } 
     } 

     const void lister_exemplaires(string langue = "none") const { 
      for(auto & book : bibil){ 
       if((book->getLangue() == langue)) 
        cout << book->getTitre() << endl; 
       // TODO display books in a chosen language or all if none 
      } 
     } 
     int compter_exemplaires(Oeuvre &livre) const{ 
      int compt = 0; 
      for(auto * book : bibil){ 
       // TODO display how many copy of livre 
       if(book->getTitre() == livre.getTitre()) 
        compt++; 
      } 
      return compt; 
     } 

     void afficher_auteurs(bool prix = false) const{ 
      for(auto * book : bibil){ 
       // TODO display auteur names based on prix true or false 
      } 
     } 

     ~Bibliotheque() { 
      cout << "La bibliothèque " << name << " ferme ses portes,"<< endl; 
      cout << "et détruit ses exemplaires :" << endl; 
     } 
    }; 

    int main() 
    { 
     Auteur a1("Victor Hugo"), 
     a2("Alexandre Dumas"), 
     a3("Raymond Queneau", true); 

     Oeuvre o1("Les Misérables "   , a1, " français"), 
     o2("L'Homme qui rit "   , a1, " français"), 
     o3("Le Comte de Monte-Cristo " , a2, " français"), 
     o4("Zazie dans le métro "  , a3, " français"), 
     o5("The Count of Monte-Cristo ", a2, " anglais"); 

     Bibliotheque biblio("public"); 
     biblio.stocker(o1, 2); 
     biblio.stocker(o2); 
     biblio.stocker(o3, 3); 
     biblio.stocker(o4); 
     biblio.stocker(o5); 

     cout << "The library contains :"<< endl; 
     biblio.lister_exemplaires(); 

     cout << "The books is english are :" << endl; 
     biblio.lister_exemplaires("anglais"); 

     cout << "Award authors are:" << endl; 
     biblio.afficher_auteurs(true); 

     cout << biblio.compter_exemplaires(o3)<< " cpoy of Monte christo (should be 3)"<<endl; 

     cout << "o4 title is " << o4.getTitre()<< "(should be Zazie) :" << endl; 

     return 0; 
    } 
+3

「著者」のコンストラクタの名前のスペルを間違えました。 –

+0

コンストラクタ名はクラス名に一致する必要があります。また、別のオブジェクトへの参照を保持する1つのオブジェクトは、設計上の質問です。 – basav

+0

ありがとうございます。クラス、属性、コンストラクタなどは別の言語で書かれていましたが、私はそれらを翻訳して少しの文脈を与えました。私はそれを忘れてしまった。 –

答えて

1

あなたは著者クラスにプライベート基準部材を初期化していなかったので、このコードはコンパイルされていません。あなたの初期化リストを修正してください。と

Book(string title, const Author &author, string language) 
    : title(title), author(author), language(language) {} 
+0

この方法で試しましたが、端末にコンテンツを出力しようとすると別の問題が発生します。 ** ror: 'const string'の 'this'引数としてAuthor 'getName()'が修飾子を破棄します** _ Bookクラスで必要なメソッドは次のとおりです:_ ' void display )const {cout << title << "、" << Author.getName()<< "、" en "<< language << endl; } ' –

0

本のクラスはAuthor &authorのプライベートである必要はありません変数の操作はプライベートで制限されています。 author.getName()を呼び出すときのエラーについては、私はあなたがそれを呼び出した場所のエラーであると確信しています。クラス内の変数を設定するctor-initializationであり、その位置の変数がポインタであり、getName() std :: stringを返します。不一致があります。私はconst Author &authorをメモリ内のスペースを指すようにしても、実際にどこを指すのかを教えてくれないので、エラーが出ると思います。それは何かのようにすべきであるconst Author &author = nullptr;

サイドノート:私が見ることができるからusing namespace stdを使用しています。あなたはそれをやるべきではありません。あなたがその行を使うときには、自分自身を大量のエラーや頭痛にまで広げています。なんらかの理由で簡単にしたい場合は、ヘッダーまたはソースファイルの先頭にusing std::stringという行を書くことができます。それで、今使っているのと同じ方法で使うことができます。

+0

Bookクラスのコンストラクタを 'Book(string title、const Author&author、string language) :title(タイトル)、author(著者)、language(言語){}'で変更しました。端末にコンテンツを出力する方法がある。 'void display()const {cout << title <<"、 "<< Author.getName()<<"、 "en" << language << endl; } 'それは別のエラーメッセージを生成する_error: 'const Auteur'を 'const'の 'this'引数として渡すAuteur :: getNom()は修飾子を破棄します –

+0

あなたの' getName() 'と関係していると思います。参照やポインタを返さないので、それが一定である必要はありません。それが問題でなければ、実際には必要ではないディスプレイ上の 'const'かもしれません。何らかの理由でそれらのどちらも問題でない場合は、 'author'変数のconstを取り除きます。なぜなら、それは必要ではない/実際には定数ではないからです。また、メソッドを 'author.getName()'として表示する必要がありますが、翻訳からのエラーであると想定しています。 – matt2405

関連する問題