user229574
user229574

Reputation:

const error message

what does this error message mean : error C2662: 'addDoctor' : cannot convert 'this' pointer from 'const class Clinic' to 'class Clinic &' I'm not using const though

sorry,but this is my first time posting experience

Clinic.h

class Clinic{
    public:
        Clinic();
        int addDoctor(Doctor*); 
    private:
        Doctor doctors[10];
        int d;
    };

Clinic.cpp

Clinic::Clinic()
    {d=-1;}
int Clinic::addDoctor(Doctor *x)
    {d++;doctors[d]=x;return d;} 

Upvotes: 1

Views: 726

Answers (4)

Naveen
Naveen

Reputation: 73433

That means you are trying to call non-const member function addDoctor from a const member function.

EDIT: In the newly posted code, you have an array of Doctor objects but you are trying to put an pointer into that array. That will give an error like can not convert from Doctor* to Doctor

Upvotes: 1

aJ.
aJ.

Reputation: 35450

MSDN example

// C2662.cpp
class C {
public:
   void func1();
   void func2() const{}
} const c;

int main() {
   c.func1();   // C2662
   c.func2();   // OK
}

Upvotes: 0

Goz
Goz

Reputation: 62323

It means you are calling addDoctor on a const this without the function call being a const.

The variable you are calling addDoctor on needs to NOT be const.

Edit: A const member function looks like this:

class MyClass
{
public:
   void MyNonConstFunction();
   void MyConstFunction() const;
};

const guarantees that you don't change anything inside the class. I assume with addDoctor that is not the case.

Most likely you have something like the following

const Object obj;
obj.addDoctor();

This will break. If you do:

Object obj;
obj.addDoctor();

Things will work. Of course this gets more complex if you are calling it on the const return from a function. If this is the case you need to get that object a different way.

Upvotes: 2

Nikola Smiljanić
Nikola Smiljanić

Reputation: 26873

You have a const object and you are trying to call non const member function. All errors are documented.

Upvotes: 3

Related Questions