User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 402,410 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,072 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 176 | Replies: 3 | Solved
Reply
Join Date: Dec 2007
Posts: 218
Reputation: henpecked1 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 1
henpecked1 henpecked1 is offline Offline
Posting Whiz in Training

Getting a constant conversion error

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

#include <iostream>
using namespace std;

class costOfItem 
{
         public:
	costOfItem(float i_cost);
	void printMe();

          private:
	  const float price;

};
// Free functions
void displayPrice(costOfItem z)  { z.printMe(); }
void displayRefPrice(costOfItem &z) { z.printMe(); }
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?
AddThis Social Bookmark Button
Reply With Quote  
Join Date: May 2007
Location: Ireland
Posts: 1,698
Reputation: twomers will become famous soon enough twomers will become famous soon enough 
Rep Power: 6
Solved Threads: 25
twomers's Avatar
twomers twomers is offline Offline
Posting Virtuoso

Re: Getting a constant conversion error

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

Re: Getting a constant conversion error

  #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 5:43 pm.
Reply With Quote  
Join Date: Jun 2007
Posts: 188
Reputation: dougy83 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 30
dougy83 dougy83 is offline Offline
Junior Poster

Re: Getting a constant conversion error

  #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  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 2:01 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC