?
Solved

Writing a stl string to the screen

Posted on 2001-06-07
7
Medium Priority
?
210 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 800 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

764 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