simple 'string' question

hmahesh1
hmahesh1 used Ask the Experts™
on
Hello,
        Can somebody tell me why the following code gives me a syntax error that says 'C:\Cplusplus\bu course\9th week\first.cpp(9) : error
 C2679: binary '<<' : no operator defined which takes a
 right-hand operand of type 'class
 std::basic_string<char,struct
 std::char_traits<char>,class std::allocator<char> >'
 (or there is no acceptab le conversion)' ?

The code is :


#include <iostream>
#include <string.h>
using namespace std;

 void main ()
 {
 string name;
 name = "hari";
 cout  << name;
 }

No matter whether I say #include <string> or #include <string.h>, I am getting syntax error.
I am using MS Visual C++ ver 6.0.

Any help will be greatly appreciated.

Thankyou,
hmahesh1



Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Since you are using STL, try taking the .h out of string.h. I tried this on my school's VAX/VMS compiler and the error went away. STL libraries don't use the .h extension.

Larry
This works fine for me in MSVC++ v6.0

#include <iostream>
#include <string>
using namespace std;

void main()
{
     string name;
     name = "hari";
     cout  << name;
}

Cheers!

Author

Commented:
Like I specified in the question, I tried both <string> as well as <string.h>. Nothing worked out for me.
OWASP Proactive Controls

Learn the most important control and control categories that every architect and developer should include in their projects.

make sure you have the string.h header file in /include directory......
Sorry, didn't see that. You have some other error then. Try adding .h to iostream. I don;t see any other syntax errors though. You might also try retyping the code entirely. Sometimes non printable characters get in your code and wreak havoc with your compiler and create errors that, according to your code, shouldn't be there.

Commented:
to see nonprintable code, press Ctrl-Shift-8

press Ctrl-Shift-8 to hide them

Commented:
I didn't know cout took string. When did that happen?

cout << name.c_str();

will work.. and.. well..

Commented:
what you need is to include <sstream>

#include <sstream>
#include <iostream>
#include <string>

using namespace std;

now  cout << name;

will work

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial