Thread: incompatible types in assignment of 'char*'

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    May 2017
    Posts
    129

    incompatible types in assignment of 'char*'

    I am trying to compile program given in link C++/Classes and Inheritance - Wikiversity but it gives error

    Code:
    #include<iostream>
    
    class Dog
    {
    private:
        char name[25];
        int gender;
        int age;
        int size;
        bool healthy;
    
    
    public:
        char* getName()   { return name;   }
        int   getGender() { return gender; }
        int   getAge()    { return age; }
        int   getSize()   { return size; }
        bool  isHealthy() { return healthy; }
        void  setHealthy(bool dhealthy) { healthy = dhealthy; }
        void  setName(char* dname)      { name = dname; }
    };
    
    
    int main()
    {
        Dog lucy;
    
    
        std::cout << "lucy's name   is " << lucy.getName()   << std::endl;
        std::cout << "lucy's gender is " << lucy.getGender() << std::endl;
        std::cout << "lucy's age    is " << lucy.getAge()    << std::endl;
        std::cout << "lucy's size   is " << lucy.getSize()   << std::endl;
        if(lucy.isHealthy())
            std::cout << "lucy is healthy"       << std::endl;
        else
            std::cout << "lucy isn't healthy :(" << std::endl;
    
    
        std::cout << "Now I'm changing lucy abit..." << std::endl;
    
    
        lucy.setHealthy(!(lucy.isHealthy()));
        lucy.setName("lUCY");
    
    
        std::cout << "lucy's name   is " << lucy.getName()   << std::endl;
        std::cout << "lucy's gender is " << lucy.getGender() << std::endl;
        std::cout << "lucy's age    is " << lucy.getAge()    << std::endl;
        std::cout << "lucy's size   is " << lucy.getSize()   << std::endl;
        if(lucy.isHealthy())
            std::cout << "lucy is healthy"       << std::endl;
        else
            std::cout << "lucy isn't healthy :(" << std::endl;
    
    
        return 0;
    }
    In member function 'void Dog::setName(char*)':
    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");
    ^
    Last edited by abhi143; 11-20-2019 at 07:18 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 06-01-2015, 07:13 AM
  2. incompatible types in assignment
    By tetartos in forum C Programming
    Replies: 9
    Last Post: 11-27-2011, 09:39 AM
  3. incompatible types in assignment
    By Adam Rinkleff in forum C Programming
    Replies: 2
    Last Post: 07-13-2011, 01:10 AM
  4. Incompatible types in assignment.
    By killpoppop in forum C Programming
    Replies: 36
    Last Post: 12-22-2008, 12:08 PM
  5. incompatible types in assignment
    By vapanchamukhi in forum C Programming
    Replies: 6
    Last Post: 09-19-2008, 07:45 AM

Tags for this Thread