1

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;} 
8
  • Posting some code might help. Commented Dec 11, 2009 at 11:24
  • if there's a way to upload the source file that would be helpful but I'm new here I don't know how to post it .So if anyone knows let me know Commented Dec 11, 2009 at 11:26
  • Just insert the code snippet into the question and format it useing the "code" button on the toolbar. Commented Dec 11, 2009 at 11:27
  • @fresh-girl: Just post the relevant code into your question. Make it as long as necessary and as short as possible. To format the code correctly indent it with four spaces. The complete reference: stackoverflow.com/editing-help Commented Dec 11, 2009 at 11:30
  • A bit of code would probably help a lot with getting to your problem ... Commented Dec 11, 2009 at 11:30

4 Answers 4

3

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

Sign up to request clarification or add additional context in comments.

Comments

2

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.

3 Comments

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 this class some{ private Doctor array[10]; public void addDoctor(Doctor) }; void main(){ Doctor x; array.addDoctor(x); }
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
1

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

Comments

0

MSDN example

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

Comments