473,413 Members | 1,789 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,413 software developers and data experts.

Problem with const reference as a parameter

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
2 1075
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
Daniel T. wrote:
make the above line:

std::string ReturnValueAsString() const


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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Emanuel Ziegler | last post by:
Hello, I want to do some mathematics with functions. In my case the function classes are very complex, but this simple example has the same problems. To allow calculations that begin with a...
3
by: ded' | last post by:
Hello ! I've read in a magazine "reference parameter in operator= must be const, because in C++, temporary objects are const" and then my operator would not work with temporary objets. But,...
16
by: Steven T. Hatton | last post by:
In the following code, the only way I can figure out to pass an array of const is by setting the template argument to const in the instanciation expression. It would be (or seem to me) better if I...
7
by: Richard Cavell | last post by:
Hi, The point of using const on a parameter to a function should be to let your compiler know that the parameter shouldn't be modified during your program. This allows you to keep your code...
8
by: SpOiLeR | last post by:
Hello! I have a matrix class like this: class MyObject; // MyMatrix is contains MyObjects class MyMatrix { public: ...
0
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen....
9
by: burningsunorama | last post by:
Hi guys! This is maybe a too 'academic problem', but I would like to hear your opinions, something like pros and cons for each approach.... ... Recently we've had at work a little talk about the...
16
by: hzmonte | last post by:
Correct me if I am wrong, declaring formal parameters of functions as const, if they should not be/is not changed, has 2 benefits; 1. It tells the program that calls this function that the...
11
by: venkatagmail | last post by:
I have problem understanding pass by value and pass by reference and want to how how they are or appear in the memory: I had to get my basics right again. I create an array and try all possible...
16
by: PeterAPIIT | last post by:
Hello all C++ expert programmer, i have wrote partial general allocator for my container. After reading standard C++ library and code guru article, i have several questions. 1. Why...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.