[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Writing a stl string to the screen

Posted on 2001-06-07
7
Medium Priority
?
213 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
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.

656 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