c++ - Constructor with a array type of another class -
this photo of model have resolve:
i have class:
#include<iostream> #include<fstream> using namespace std; class word { protected: char *value; char type[20]; int nochars; static int nowords; public: word(char *value, char *type) { this->nochars = 0; this->value = new char[strlen(value) + 1]; strcpy(this->value, value); strcpy(this->type, type); word::nowords++; } word() { this->nochars = null; this->value = null; strcpy(this->type,"nedeterminat"); } void operator=(word &x) { this->nochars = x.nochars; strcpy(this->type, x.type); this->value = new char[strlen(x.value) + 1]; strcpy(this->value, x.value); } word(const word& x){ this->nochars = x.nochars; strcpy(this->type, x.type); this->value = new char[strlen(x.value) + 1]; strcpy(this->value, x.value); } char* getvalue() { return this->value; } void settype(char* x) { if (x == null) { throw new exception("tip gresit!"); } else { strcpy(this->type, x); } } char &operator[](int i) { if (i >= 0 && <= (strlen(this->value) - 1)) { return this->value[i]; } else cout << endl << "eroare indice: " << i; } static int getnowords() { return word::nowords; } operator int() { return this->nochars; } friend ostream& operator<<(ostream&, word&); friend istream& operator>>(istream&, word&); }; ostream& operator<<(ostream& consola, word& x) { consola << "value: " << x.getvalue() << endl; consola << "type: " << x.type << endl; consola << "nochars: " << x.nochars << endl; return consola; } istream& operator>>(istream& consola, word& x){ cout << "value: "; consola >> x.value; cout << "type: "; consola >> x.type; cout << "nochars: "; consola >> x.nochars; return consola; } int word::nowords = 0; class dictionary{ private: char *language; int nowords; bool isonline; word v[100]; public: dictionary(char *language, word w, int nowords, bool isonline) { this->language = new char[strlen(language) + 1]; strcpy(this->language, language); (int = 0; < 100; i++) { this->v[i] = w; } this->nowords = nowords; this->isonline = isonline; } }; int main() { //1 word w1("exam", "noun"); /*word w2;*/ word w3 = w1; cout << w3; //2 cout << endl << "word value: " << w3.getvalue(); word w2("to take", "noun"); w2.settype("verb"); //3 w3 = w2; cout << endl << w3; word *pw = new word("pointer", "noun"); delete pw; //4 cin >> w3; cout << w3; char character = w3[2]; cout << endl << character; //5 double nochars = (int)w1; cout << endl << nochars; cout << endl << word::getnowords() << endl; //6 dictionary dictionary1("english", null, 0, false); }
i have main:
dictionary dictionary1("english", null, 0, false);
how should change constructor work? receive error :
arrgument types are:(const char[8],int,int,bool);
and how should write default constructor?
null
cannot assignedword
. tryword()
instead call default constructorword
. consider changing function parameterconst word&
- anonymous temporaries allowed bindconst
references.i'd prefer see
std::string
type member variablelanguage
; usingconst std::string&
function parameter. not have worry subsequentdelete
call, , defining own assignment operators , copy constructors. currently, class leaks memory.
Comments
Post a Comment