Incompatible types in assignment of char to char là gì năm 2024

In your setName[] function you should first verify that the passed C-string is small enough to fit into the name array, then use strcpy[] to copy the passed string into the name array.

Also do your realize that you're trying to print your uninitialized class variables on lines 29 thru 36?

All of your getXXXX[] member functions should be const qualified.

Code:

...

int   getGender[] { return gender; } const  
...

Lastly, for now, you really should find a different source. The one you've found seems to have quite a few deficiencies and is really doing you a disservice.

Edit: I wonder why gender is an int instead of either a string or a single char.

  • 11-20-2019
    Registered User ---
    Lastly, for now, you really should find a different source. The one you've found seems to have quite a few deficiencies and is really doing you a disservice.

Not really, Wikiversity is a really good source to learn from. @abhi143 rewrote the code in a different way, probably for practice, and that's what has produced him his errors. You can see the website for yourself, it's pretty good for someone starting out on classes and inheritance and abstraction.

Also do your realize that you're trying to print your uninitialized class variables on lines 29 thru 36?

Wikiversity: However, if we run this program, we might have a little problem. We have never initialized lucy's properties...

Well, they try and teach about initialization through this. You probably didn't look through the website but that's not your fault. It's a really reliable place to learn from.

Code:

In member function 'void Dog::setName[char*]':

Code:

error: incompatible types in assignment of 'char*' to 'char [25]' void setName[char* dname] { name = dname; }

In function 'int main[]': warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings] lucy.setName["lUCY"];

You can't set one pointer to another pointer... Use std::string as mentioned [and written] on the website instead of experimenting with C-style strings if you're wanting to learn C++. Or, if you're trying to convert the C++ code to C code, you should do what @jimblumberg suggested.

-
  • 11-20-2019

    Registered User

    -
    Originally Posted by jimblumberg

You should really consider using std::string instead of the error prone C-strings..

Code:

includeusing namespace std;

class Dog { private: char name[25]; int gender; int age; int weight; public: char* getName[] { return name; } int getGender[] { return gender; } int getAge[] { return age; } int getWeight[] { return weight; } }; int main[] { Dog lucy; cout

Chủ Đề