tags:

views:

94

answers:

4

hi 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;}
+3  A: 

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

Nikola Smiljanić
+2  A: 

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.

Goz
that's the problem.The object I'm using is not const.to be more clear it an array of objects as a data member of the class that contain that method
as you said the method here is not const that's right but the object is close to thisclass some{ private Doctor array[10]; public void addDoctor(Doctor)};void main(){Doctor x;array.addDoctor(x);}
Well that doesn't make much sense ... because array is not const ...
Goz
th problem was that addDoctor was called in another method which is const.funny ha?! I don't know why I've put const there I don't even remember I did.thanks for your help
A: 

MSDN example

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

int main() {
   c.func1();   // C2662
   c.func2();   // OK
}
aJ
+1  A: 

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

Naveen
thanks.that was the problem
but the posted code has some totally different problem..
Naveen