'C++ Visual 2019 error const char incompatible

I get this error :

Das Argument vom Typ ""const char *"" ist mit dem Parameter vom Typ ""char *"" inkompatibel.

The argument from type const char is with the parameter from type char incompatible.

Animal::Animal(const char* k )
{
    next = 0;
    kind = new char[strlen(k) + 1];
    strcpy(kind,k);
}


Solution 1:[1]

This error occurs if the kind data member is defined as type const char *:

  error: invalid conversion from 'const char*' to 'char*' [-fpermissive]

Defining the kind data member of type char * solves the problem:

#include <iostream>
#include <cstring>

using namespace std;

class Animal
{
    public:
        Animal(const char *k)
        {
            next = 0;
            kind = new char[strlen(k) + 1];
            strcpy(kind, k);
        }
        
        const char* getKind() const
        {
            return kind;
        }
    private:
        int next;
        char *kind; /* The kind data member is defined as type "const *". */
};

int main()
{
    const char *k = "Cat";
    Animal animal(k);
    cout << "Kind: " << animal.getKind() << endl;

    return 0;
}

Result:

Kind: Cat

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1