Getting a constant conversion error

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Dec 2007
Posts: 226
Reputation: henpecked1 is an unknown quantity at this point 
Solved Threads: 1
henpecked1 henpecked1 is offline Offline
Posting Whiz in Training

Getting a constant conversion error

 
0
  #1
May 9th, 2008
I'm having a little problem with a conversion error during compile. Here is the code:

  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?
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 1,871
Reputation: twomers has a spectacular aura about twomers has a spectacular aura about twomers has a spectacular aura about 
Solved Threads: 56
twomers's Avatar
twomers twomers is offline Offline
Posting Virtuoso

Re: Getting a constant conversion error

 
0
  #2
May 9th, 2008
Maybe try a:
  1. void printMe const {
  2. // Code here...
  3. }
I blag!?
"Mr Kitty, you have to live in the attic now. Here, write a diary."
I am the Walrus!
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 226
Reputation: henpecked1 is an unknown quantity at this point 
Solved Threads: 1
henpecked1 henpecked1 is offline Offline
Posting Whiz in Training

Re: Getting a constant conversion error

 
0
  #3
May 9th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 275
Reputation: dougy83 is on a distinguished road 
Solved Threads: 45
dougy83 dougy83 is offline Offline
Posting Whiz in Training

Re: Getting a constant conversion error

 
0
  #4
May 9th, 2008
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.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC