By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,851 Members | 2,104 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,851 IT Pros & Developers. It's quick & easy.

Problem with const reference as a parameter

P: n/a
Short question is here:
I would like to have the code below to compile, without having to remove
the "const" from it, can anyone help?

(More explanation can be found at the end of the message if that is needed)

---------code.cpp-------------
#include <sstream>
#include <iostream>

class ValueHolder
{
private:
unsigned int value;

public:
ValueHolder()
{
value = 40;
}

std::string ReturnValueAsString()
{
std::ostringstream oss;
oss << value;
return oss.str();
}
};

void PrintValue( const ValueHolder &holder )
{
std::cout << holder.ReturnValueAsString() << std::endl; // Line 25
}

int main()
{
ValueHolder holder;
PrintValue( holder );

return 0;
}

---------code.cpp-------------

-------Error message----------
C:\code.cpp(25) : error C2662: 'ReturnValueAsString' : cannot convert
'this' pointer from 'const class ValueHolder' to 'class ValueHolder &'
Conversion loses qualifiers
-------Error message----------

I have a function that looks like this:
void PrintValue( const ValueHolder &holder )
{
std::cout << holder.ReturnValueAsString() << std::endl;
}

But it doesn't work. It works, if I remove the const, or it works if I
remove the holder.ReturnValueAsString() from it. It would also work if I
add public: std::string str; to the ValueHolder class, set a value for
it and call it from function like this: holder.str

But that is difficult in my real situation, because I have a class where
I have many variables and they change often. So I would prefer the
possibility to create a string using std::ostringstream and then
returning the result from that from my class.

And I would like to have it so that I can use the reference of the class
as a const in my functions.
Jul 22 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
In article <4b**************@read3.inet.fi>,
Aggro <sp**********@yahoo.com> wrote:
Short question is here:
I would like to have the code below to compile, without having to remove
the "const" from it, can anyone help?

(More explanation can be found at the end of the message if that is needed)

---------code.cpp-------------
#include <sstream>
#include <iostream>

class ValueHolder
{
private:
unsigned int value;

public:
ValueHolder()
{
value = 40;
}

std::string ReturnValueAsString()
make the above line:

std::string ReturnValueAsString() const {
std::ostringstream oss;
oss << value;
return oss.str();
}
};

void PrintValue( const ValueHolder &holder )
{
std::cout << holder.ReturnValueAsString() << std::endl; // Line 25
}

int main()
{
ValueHolder holder;
PrintValue( holder );

return 0;
}

Jul 22 '05 #2

P: n/a
Daniel T. wrote:
make the above line:

std::string ReturnValueAsString() const


Thanks, that solved it!
Jul 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.