Solved

Writing a stl string to the screen

Posted on 2001-06-07
7
206 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Eclipse MARs unable to start with exit code=13 2 76
Copying WordPress Pages 5 83
computer science syllabus 3 82
CKEditor - will not function 7 70
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

773 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