私は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;
}
「著者」のコンストラクタの名前のスペルを間違えました。 –
コンストラクタ名はクラス名に一致する必要があります。また、別のオブジェクトへの参照を保持する1つのオブジェクトは、設計上の質問です。 – basav
ありがとうございます。クラス、属性、コンストラクタなどは別の言語で書かれていましたが、私はそれらを翻訳して少しの文脈を与えました。私はそれを忘れてしまった。 –