943,923 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 534
  • C++ RSS
May 9th, 2008
0

Getting a constant conversion error

Expand Post »
I'm having a little problem with a conversion error during compile. Here is the code:

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class costOfItem
  5. {
  6. public:
  7. costOfItem(float i_cost);
  8. void printMe();
  9.  
  10. private:
  11. const float price;
  12.  
  13. };
  14. // Free functions
  15. void displayPrice(costOfItem z) { z.printMe(); }
  16. void displayRefPrice(costOfItem &z) { z.printMe(); }
  17. void displayConstRef(const costOfItem &z) { z.printMe(); }
Now in the last line it gives me the error cannot convert 'this' pointer from const costOfItem to costOfItem conversion loses qualifiers. I'm assuming there is a problem with the current instance of the object (which I believe is what 'this' points to, please correct me if I'm wrong about that) and it seems to be trying to convert it to a non const. What seems to be the problem and how do I fix it?
Similar Threads
Reputation Points: 10
Solved Threads: 2
Posting Whiz in Training
henpecked1 is offline Offline
244 posts
since Dec 2007
May 9th, 2008
0

Re: Getting a constant conversion error

Maybe try a:
C++ Syntax (Toggle Plain Text)
  1. void printMe const {
  2. // Code here...
  3. }
Reputation Points: 453
Solved Threads: 57
Posting Virtuoso
twomers is offline Offline
1,873 posts
since May 2007
May 9th, 2008
0

Re: Getting a constant conversion error

Do you mean to add the const portion in the member function definition of printMe? And am I right about the 'this' portion of what I posted?
Last edited by henpecked1; May 9th, 2008 at 6:43 pm.
Reputation Points: 10
Solved Threads: 2
Posting Whiz in Training
henpecked1 is offline Offline
244 posts
since Dec 2007
May 9th, 2008
0

Re: Getting a constant conversion error

Both the member function declaration and definition will require the const modifier. This tells the compiler that the function is an 'inspector' function and will not modify anything in the object (which is required when using a const object).

About your 'this' question, 'this' is a pointer to the instance of the object.
Reputation Points: 85
Solved Threads: 45
Posting Whiz in Training
dougy83 is offline Offline
275 posts
since Jun 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: C++ Bubble sort not working with array of records, does work with simple array.
Next Thread in C++ Forum Timeline: test





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC