2016-11-20 196 views
0

我正在学习C++类,在我的课程中,我需要创建一个包含“book”,“author”和“Store”类的书店。C++在类之间传递对象

我不太清楚如何在Book类中提取作者的名字时如何在类之间进行交互。

当我运行在Xcode下面一段代码我通知对全部作品的类以下错误的构造器:预期‘(’或‘{’

UDPATE 22-NOV-2016 我更新了什么我迄今所做的代码。

首先,我有问题,当我尝试从Book_copy类显示作者的名字,我简直不知道该怎么做。

我现在已经放了author.getName(),但我知道它不起作用。

第二个问题是禁用作者拷贝构造函数和Book构造函数时出现的错误。 ???

第三个问题,假设上述内容是固定的,它不会完全按照我收到的指导来构建这个程序。

Book类必须的特点:一个字符串标题,经常提及它的作者,而另一个字符串这是写在语言

这就是我在第一时间得到了拧欲以对作者的常量引用...

#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

没人?我要去这里圈。每当我找到工作时,其他的东西就可以工作了:( 下面的代码几乎可以工作,除了看起来Bibiliotheque矢量是空的。 线条太多,有些完全没用,我要去来回尝试不同的东西,使其工作...

#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

构造函数的名称应该与类名称匹配。另外一个对象持有对另一个对象的引用,那更多的是设计问题...排序组合vs聚合。 – basav

+0

谢谢。类,属性,构造函数等是用不同的语言,我翻译它们给出一些上下文。我忘了那一个。 –

回答

1

此代码不能编译,因为你没有初始化私有引用成员Author类。纠正你的初始化列表。

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

我试过这种方式,但当我尝试在终端上输出内容时,它会带来另一个问题。 **以下是我在Book类中需要的方法:_' void display(){'const Author'as'this'参数'const string Author :: getName()'丢弃限定符** )const {cout << title <<“,”<< Author.getName()<<“,en”<< language << endl; }' –

0

与这个书类有Author &author私有它不一定是必需的该变量的操作受到限制,因为它是私人的。对于你拨打author.getName()的错误,我很确定这是错误,因为你称之为ctor初始化,它是你在类中设置变量的地方,因为那个位置的变量是一个指针,你可以调用getName()返回一个std :: string是不匹配的。我想你也会遇到一个错误,因为你将const Author &author指向了内存空间,但实际上并没有告诉它指向哪里。它应该是类似于const Author &author = nullptr;

附注:从我所看到的你正在使用using namespace std。在任何情况下,你都应该这样做。当你使用这条线时,你会面对大量的错误和头痛。如果出于某种原因想要使其更容易,可以在行首或源文件的顶部写入using std::string行。然后你可以像现在使用它一样使用它。

+0

我在Book类中改变了构造函数,它使用Book(字符串标题,const作者和作者,字符串语言) :标题(标题),作者(作者),语言(语言){}',但现在我也需要以下方法输出终端上的内容。 'void display()const {cout << title <<“,”<< Author.getName()<<“,en”<< language << endl; }'这会产生另一个错误信息_error:传递'const Auteur'作为'const'字符串的'this'参数Auteur :: getNom()'丢弃qualifiers_ –

+0

我认为这可能与你的getName()有关。因为你没有返回引用或指针,所以没有理由保持不变。如果这不是问题,它可能是显示器上的“const”,并不是真的需要。如果由于某种原因,这些都不是问题,那么我会摆脱'author'变量的const,因为它不是必需的/实际上并不是常量。你也需要以'author.getName()'的形式调用显示的方法,但我认为这是翻译错误。 – matt2405