I'm using Visual C++ express 2010.

Whats wrong with this code: (A video tutorial uses this code and works fine on theirs)

#include <iostream>
    using namespace std;

    int main(){
        string myString = "hello";
        cout << myString << endl;
         return 0;
    }

???

Says: error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\ostream(679): could be 'std::basic_ostream<_Elem,_Traits> &std::operator <<<char,std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const char *)'

Recommended Answers

All 8 Replies

well #include <string>
needs to be at the top I believe

whoops lol

#include <iostring>
include <iostring>
#include <string>

But why does the video tutorial use iostream and works for him? Am I using an older version of C++?

There is no way that I can think of that the guy only used iostream. This is because strings aren't by default part of C++ you must include them. So unless part of the code wasn't shown or something I don't know how it worked for him. Can you link the video?

im using dev c++ and didnt need to declare string. its in the library already..had to use

system ("pause");
return 0;
}

this will pause the screen so you can see the output.
microsoft c++ express 2010 requires different code sequences, the same as code blocks does. thats why you are experiencing an issue

But why does the video tutorial use iostream and works for him?

You need to use both <iostream> and <string> to get this to work.

The way C++ works, if one of the headers that you include also includes another header, then that header will also be included. It's almost literally copying the contents of the #included files into the file that you include them in. This means that sometimes you don't have to include a particular header, it will just seem to work. You shouldn't rely on this though, always include all the files that you need.

this will pause the screen so you can see the output.
microsoft c++ express 2010 requires different code sequences, the same as code blocks does. thats why you are experiencing an issue

The issue is not to do with the window closing before you get to see the output. As you can see in the original post, there is a compilation error so there will never be any output.

But why does the video tutorial use iostream and works for him? Am I using an older version of C++?

Maybe so, maybe not. Technically, the old C++ standard (from 2003, C++03 or C++98) does not require the <iostream> header to include the <string> header, and most compilers would not do so. But, some compilers would. So, it is possible that the tutorial author used one of those compilers. Then, the new standard (from 2011, C++11) added some requirements that made it more likely that the <string> header will be included by the <iostream> header.

So, it boils down to the fact that there is no guarantee that the <string> header will be included by the <iostream> header (even if a particular compiler does it). And thus, you should always include the <string> header if you need the std::string class or anything related to it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.