Solved

Writing a stl string to the screen

Posted on 2001-06-07
7
203 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
Comment Utility
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
Comment Utility
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
Comment Utility
If the previous comments not sutosfied You, please post a few lines of source code.
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 7

Expert Comment

by:KangaRoo
Comment Utility
>> 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
Comment Utility
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
Comment Utility
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
Comment Utility
Per recommendation comment force/accepted by

Netminder
Community Support Moderator
Experts Exchange
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

762 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now