simple string question...

  • Thread starter Alexander Dong Back Kim
  • Start date
A

Alexander Dong Back Kim

Hi all,

I wrote a function which has a parameter of "string". When I send the
string, can I do like the below?

void Show::caller(void)
{
string str = "This object's address is " << this;
}

void Show::show_message(string str)
{
cout << str;
}

I got errors (obviously).

What would be the solution for this? Any suggestions?

Best regards,
Alex
 
I

Ian Collins

Alexander said:
Hi all,

I wrote a function which has a parameter of "string". When I send the
string, can I do like the below?

void Show::caller(void)
{
string str = "This object's address is " << this;
}

void Show::show_message(string str)
{
cout << str;
}

I got errors (obviously).

What would be the solution for this? Any suggestions?
A) solution to what B) what errors?
 
C

Christopher

Hi all,

I wrote a function which has a parameter of "string". When I send the
string, can I do like the below?

void Show::caller(void)
{
string str = "This object's address is " << this;

}

void Show::show_message(string str)
{
cout << str;

}

I got errors (obviously).

What would be the solution for this? Any suggestions?

Best regards,
Alex

I don't know what the solution is because I don't know what the
problem is.
However, I'd modify the code on my assumptions to

#include <iostream>
#include <sstream>

void Show::GetInfo()
{
// std::string does not have a << operator to my knowledge, use
std::stringstream instead
std::stringstream info;
info << "This object's address is " << this << std::endl;

std::cout << info;

// or
// show_message(info.str());
// but the second method is so simple, it isn't needed
}

void Show::show_message(const std::string & msg) const
{
std::cout << msg;
}
 
J

Jim Langston

Alexander said:
Hi all,

I wrote a function which has a parameter of "string". When I send the
string, can I do like the below?

void Show::caller(void)
{
string str = "This object's address is " << this;

This string str is a local variable to the function caller. Once caller
returns, the string goes away. You are not reutrning it as a value, or
setting a class variable, so function is basically doing nothing useful
}

void Show::show_message(string str)
{
cout << str;
}

I got errors (obviously).

What would be the solution for this? Any suggestions?

There are a few choices depending on your design decision.

1. Make str a member of the class. or
2. return str from caller, store it somewhre, then pass it in to
show_message.
3. This is not covering the second error in your code trying to use << on a
std::string

Example of 1 (untested code):

class Show
{
public:
void caller();
void show_message();
private:
std::string str;
};

void Show::caller()
{
str = "This object's address is " + "something";
}

void Show::show_message()
{
std::cout << str << "\n";
}

Now the string str is stored in the class, and as such is visible to all
functions defined in that class (as long as you don't declare another
variable named str in a function bringing up name hiding).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,794
Messages
2,569,641
Members
45,353
Latest member
RogerDoger

Latest Threads

Top