Invalid class arguments

char* n=m.getName(); 

I get the following error Invalid arguments ' Candidates are: char * getName() ' for the above statement. What am I missing?

 char* Medicine::getName() { return this->name; } 

name declared as char name[50]; and m is const Medicine& m

+4
source share
2 answers

If m is const , only const methods can be called on it. Perhaps you can change your method to

 const char* Medicine::getName() const; 

and use it as follows:

 const char* n=m.getName(); 

Although you can use the std::string data element instead of the char array.

+9
source

Note that if a member variable is const, only a member function can access it. The same for static, if a member variable is static, only a static member can access it.

0
source

Source: https://habr.com/ru/post/1479952/


All Articles