Solved

Writing a stl string to the screen

Posted on 2001-06-07
7
207 Views
Last Modified: 2013-12-14
I am trying to simply write a string which has been declared as std::string. I have a function
which returns that data type. However, when I call the function and say cout<<(function name), I get the error that there is no right hand side operator which can be used with <<. It says the right hand side is of type std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >.
I have no clue what all this means and how to fix it.

Any suggestions would be greatly appreciated.
                                         Thanks,
                                         Chris
0
Comment
Question by:drexeliu
7 Comments
 

Expert Comment

by:moorrees
ID: 6165491
the cout function expects a pointer to a char. What you can do is
give cout this.
cout << &std:string;

Or you can cast it to a char.

cout << char(std:string);

Don't forget the ';' at the end.

Remember that string is not a standard type.
Try to declare it as

char text[256];
0
 
LVL 86

Accepted Solution

by:
jkr earned 200 total points
ID: 6165547
You might want to use a C-style string:

std::string _str = "some text";

cout << _str.c_str();

Alternatively, use the 'std' namespace:

#include <string>

using namespace std;

string _str = "some text";

cout << _str;
0
 
LVL 2

Expert Comment

by:MadYugoslav
ID: 6166650
If the previous comments not sutosfied You, please post a few lines of source code.
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 7

Expert Comment

by:KangaRoo
ID: 6166728
>> cout << &std:string;
Hardly, that would print out a pointer, displayed as a pointer.

Something like
#include <string>
#include <iostream>

std::string f()
{
   return std::string("Hello");
}

int main()
{
   cout << f();
   return 0;
}

should work since
  ostream& operator << (ostream&, const std::string&)
is part of the std library.
What are you writing to?
What is your compiler? OS?
0
 
LVL 9

Expert Comment

by:jasonclarke
ID: 6166970
It may well be that you are using <iostream.h> (the old iostream library) rather than <iostream> (the new iostream library).

In VC++ at least, this produces this warning, because there is no support for std::string in the old library.

To fix it just change #include <iostream.h> to #include <iostream>
0
 
LVL 11

Expert Comment

by:griessh
ID: 6811707
I think you forgot this question. I will ask Community Support to close it unless you finalize it within 7 days.
Unless there is objection or further activity,  I will suggest to accept "jkr" comment(s) as an answer.

If you think your question was not answered at all, you can post a request in Community support (please include this link) to refund your points.
The link to the Community Support area is: http://www.experts-exchange.com/jsp/qList.jsp?ta=commspt

Please do not accept this comment as an answer!
======
Werner
0
 
LVL 5

Expert Comment

by:Netminder
ID: 6828375
Per recommendation comment force/accepted by

Netminder
Community Support Moderator
Experts Exchange
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

790 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question