Problem mit Methode
-
hab eine Frage zu der Methode set_author:
ich wollte diese so programmieren, dass diese dynamisch ist, sprich, nicht jedes Buch nur ein Autor hat, sondern auch Mal 10 oder nur 5 besitzen kann.Hat wer einen Rat?
/* This is file book.h */ // define of header #ifndef _book_h_ #define _book_h_ #include <iostream> #include <conio.h> #include <string> using namespace std; class book{ private: string title; int n_author; // char author[n_nauthor][100]; // <--- int sites; int isbn; string publisher; public: void set_title(string); void set_n_author(int); // void set_author(char); void set_sites(int); void set_isbn(int); void set_publisher(string); string gives_title(); int gives_n_author(); // char gives_author(); int gives_isbn(); int gives_sites(); string gives_publisher(); book(); ~book(); }; // setting-methods: void book::set_title(string title){ this->title=title; } void book::set_n_author(int n_author){ this->n_author=n_author; } /* void book::set_author(char author[gives_n_author()][100]){ for(int n=0;n<gives_n_author();n++) strcpy(this->author[n],author[n]); } */ void book::set_sites(int sites){ this->sites=sites; } // return-giving-methods: string book::gives_title(){ return title; } int book::gives_n_author(){ return n_author; } /* char gives_author(){ char c[100]; if(n_author==0) return "none author"; else for(int n=0;n<n_author;n++) strcpy(c[n],author[n]); return c; } */ void book::set_isbn(int isbn){ this->isbn=isbn; } void book::set_publisher(string publisher){ this->publisher=publisher; } int book::gives_isbn(){ return isbn; } int book::gives_sites(){ return sites; } string book::gives_publisher(){ return publisher; } // constructor: book::book(){} // destroyer: book::~book(){delete this;} #endif /* _book_h_ */Danke für die Hilfe

-
class book{ ... vector<string> authors; ... void add_author(string const& author){ authors.push_back(author); } ... //book::book(){}//weg, war nutzlos //book::~book(){delete this;}//weg, war falsch
-
in der Schule machen wir es aber auch so

eine classe braucht doch mindestens einen Konstruktor und darf nur einen Destukter haben, oder nicht? ô.o
-
Broody schrieb:
in der Schule machen wir es aber auch so

eine classe braucht doch mindestens einen Konstruktor und darf nur einen Destukter haben, oder nicht? ô.oSie braucht keinen von Dir geschriebenen Default-Konstruktor. Das kann der Compiler auch unsichtbar alleine.
Ebenso braucht sie keinen von Dir geschriebenen Destruktor. Und schon gar keinen, der eine endlose Rekursion anschubst, denn in delete this; wird der Destruktor aufgerufen, der seinerseite delete this; aufruft...
-
Danke, den Rest versuche ich mir nun allein bei zu bringen, ua. was vector bewirkt.
Davon hatte ich auch noch nie gehöhrt oder gelesen.