Printing out std::string - c++

In the sample code below
std::string result = exec( "dir" ) ;
cout<<result;
I get the following error
error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class std::basic_string
I suspect there is a special method to print out an std::string.
Please help me debug this.
Also, I have included iostream.h, fstream.h and stream header files.

I suspect that you need to qualify cout with std::
std::cout << result;
or add using namespace::std to the top of your cpp file.

You need to include <string>

cout is defined in <iostream>. Getting the << syntax to work with std::strings requires <sstream>.
#include <iostream>
#include <sstream>
std::string result = "something";
std::cout << result << " and something else";

Answering my own question on behalf of #MrLister since he was inactive.
I should have included <iostream> and <fstream> without .h. Also using namespace std; should have been typed after that.
Ex:
#include <string>
#include <iostream>
#include <fstream>
#include <stdlib>
using namespace std;
Many many thanks to #MrLister.
And thanks to #dasblinkenlight. His answer enhanced a little bit.

Related

std::string with ostream_iterator [duplicate]

Here's my code, how do I fix this error?
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
string title = "THE WORLD OF PIRATES";
cout << title << endl;
cout << " Welcome to the world of pirates";
cin.get();
return 0;
}
The error is
binary '<<' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)
You forgot to #include <string>
using std::string without including it's header works on some compilers that indirectly import parts of <string> into their <iostream> or other headers but that's not standard and shouldn't be relied upon. Also they often break when you try to output a string since they only included a part of the implementation and are missing the part that implements the operator<<.

Insertion operator is not working with vector and I don't know why

So I was starting to write my code and I was going to test to see if I still remember how to cast, until I get a red line under my operator.
This is the compiler error:
Error C2679: binary '<<': no operator found which takes a right-hand operand of type 'std::basic_string<char,std::char_traits<char>,std::allocator<char>>' (or there is no acceptable conversion) (12)
I honestly never had a problem with outputting a string/vector so I do not know how to fix this. Can someone please tell me how to fix this. It would also be awesome if you could tell me what is wrong with the code.
#include "stdafx.h"
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<string>hello;
hello.push_back("9");
for (auto i : hello)
cout << i << " "; <-- The first operator is underlined. Why?
return 0;
}
You need one more include in your program:
#include <string>
While <iostream> does declare/define some string related functions, not all of them.
With some compilers, the iostream header incldues string internally, but that isn't required by the standard - and Visual Studio doesn't, that's why you receive this error.

Building in Visual Studio not recognizing getline() or stoi() even with #include <string> and <fstream> statements

I've been trying to learn C++ over the past couple of days, and ran into a problem when I was trying to use the getline() and stoi() methods in some practice code:
#include <string>
#include <fstream>
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
string numberGuessed;
int intNumberGuessed = 0;
do {
cout << "Guess a number between 1 and 10";
getline(cin, numberGuessed);
intNumberGuessed = (stoi(numberGuessed));
cout << intNumberGuessed << "\n";
} while (intNumberGuessed != 4);
cout << "You win\n";
return 0;
}`
When I tried to build this code in VS 2015, the console could not identify getline or stoi as if I hadn't added #include statements for string and fstream. Is there something wrong with my code or is it something to do with VS?
It's something to do with VS.
Since you have
#include "stdafx.h"
I'm guessing you have precompiled headers turned on, and "stdafx.h" is the precompiled header. (That's the default name in VS)
With precompiled headers turned on, anything before the include statement for the precompiled header is ignored.
Either make sure #include "stdafx.h" is the very first thing in the file (except for comments), or turn off precompiled headers.

Visual C++ No operator ">>" matches these operands

I have been searching around for a solution to this for a while. I'm trying to use the ">>" and "<<" operators as shown below but keep getting the error "No operator '>>' matches these operands". Here are the code snippets:
#include "stdafx.h"
#include <iostream>
#include <cmath>
#include <fstream>
#include <string>
...
int blah;
std::string level;
level >> blah;
thisDot.setX(blah);
level >> blah;
thisDot.setY(blah);
Several of the posts I have found on this topic have had the solution of simply including #include <string>, but as you can see I have included that and am still getting the error. I have also tried #include string.h but that doesn't help either. Any ideas as to why this would still be happening?...it may be worth noting that I'm using the SDL library.
You misread the example. The example is reading from a file, not a string.
std::string level;
// ...
load >> offset;
Notice that it is load that appears on the left of the >>, not level.

std has no member 'getline'?

I'm trying to use std::getline, but my compiler is telling me that getline isn't identified?
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <fstream>
#include <cstdlib>
int main(){
using namespace std;
string line;
ifstream ifile("test.in");
if(ifile.is_open()){
while(ifile.good()){
getline(ifile,line);
}
}
}
std::getline is defined in the string header.
#include <string>
Also, your code isn't using anything from cstring, cstdio, cmath, or cstdlib; why bother including these?
EDIT: To clarify the confusion regarding the cstring and string headers, cstring pulls the contents of the C runtime library's string.h into the std namespace; string is part of the C++ standard library and contains getline, std::basic_string<> (and its specializations std::string and std::wstring), etc. -- two very different headers.
As ildjarn points out, the function is declared in <string>, and I'm suprised you didn't get an error at:
string line;
Also, this:
while(ifile.good()){
getline(ifile,line);
}
is not the way to write a read loop. You MUST test the success of the read operation, not the current stream state. You want:
while( getline(ifile,line) ) {
}
this is happening because getline comes from the string library, you need to #include <string> or #include <cstring>