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<<.
Related
I am trying to search for files with certain extensions in a directory, using the "recursive_directory_iterator" function inside library.
I am using Visual Studio Express 2017.
I am following the code in this answer:
https://stackoverflow.com/a/47975458/4145697
Here is my code:
#include <windows.h>
#include <iostream>
#include <filesystem>
#include <string>
void get_list_of_files(void)
{
std::string constructed_path_str_dbg = "C:\\Cpp_trials\\Trials\\Debug\\baseline\\cpp_files_trial";
std::string ext(".sample");
for (auto& p : fs::recursive_directory_iterator(constructed_path_str_after))
{
if (p.path().extension() == ext()) // errors E0980 and C2064
std::cout << p << '\n'; // errors E0349 and C2679
}
}
But I am having the following compilation errors:
E0980 call of an object of a class type without appropriate operator() or conversion functions to pointer-to-function type
E0349 no operator "<<" matches these operands
C2064 term does not evaluate to a function taking 0 arguments
C2679 binary '<<': no operator found which takes a right-hand operand of type 'const std::filesystem::directory_entry' (or there is no acceptable conversion)
According to the code you provided, I tested and modified it.
Change constructed_path_str_after to constructed_path_str_dbg
Change ext() to ext
The following are my test results:
#include <fstream>
#include <iostream>
#include <experimental/filesystem>
namespace fs = std::experimental::filesystem;
int main()
{
std::string constructed_path_str_dbg = "C:\\Cpp_trials\\Trials\\Debug\\baseline\\cpp_files_trial";
std::string ext(".txt");
for (auto& p : fs::recursive_directory_iterator(constructed_path_str_dbg))
{
if (p.path().extension() == ext)
std::cout << p << '\n';
}
return 0;
}
I hope to know why you would use constructed_path_str_after, because this is related to your problem solving. I can only speculate on your needs based on the existing code.
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.
im having a problem when trying to print out pairs of int & strings in an STL Map:
this is the code im using:
#include <iostream>
#include <utility>
#include <map>
using namespace std;
typedef map<int,string> intID;
int main(){
intID ID;
ID.insert(pair<int,string>(123,"studentname1"));
ID.insert(pair<int,string>(124,"studentname2"));
ID.insert(pair<int,string>(122,"studentname3"));
intID::iterator IDIter;
for(IDIter = ID.begin();IDIter != ID.end();++IDIter){
cout <<"ID: " << IDIter->first <<", Name: " << IDIter->second << endl;
}
}
The error occurs at the ", Name: " << IDIter->second part, the << is underlined saying "no operator matches these operands"
the compile error is:
Error 1 error C2679: binary '<<' : no operator found which takes a
right-hand operand of type 'std::string' (or there is no acceptable
conversion)
Im trying to print out the second member of the pair (studentname)
I'm new to STL mapping so i'm not sure what i'm doing wrong, what do i need to change?
You need to include the <string> header. You've only been able to use the std::string type by incidental inclusion from other headers. You can't rely on this. Including <string> will also bring in the overloads of operator<< that allow you to output strings.
I have a basic program that compares two strings :
#include <string>
#include <iostream>
using namespace std;
int main (int argc, char *argv[]) {
if(strcmp (argv[0],"./test") != 0) {
cout << "not equal" << endl;
} else {
cout << "equal" << endl;
}
return 0;
}
it compiles with gcc but not with clang :
> clang -o test test_clang.cpp
test_clang.cpp:7:6: error: use of undeclared identifier 'strcmp'
if(strcmp (argv[0],"./test") != 0) {
^
1 error generated.
Why doesn't it compile with clang ?
EDIT: People are getting harsh on stack overflow, up to the point that I am hesitating to post a question. The question above has a simple answer, fine, but is it normal to down-vote questions (twice in the first minute!) because they have a simple, yet non obvious, answer ?
Use
#include <string.h>
or
#include <cstring>
instead of
#include <string>
The string header is for the std::string from C++. string.h is for C zero terminated char* strings. cstring is like string.h but for C++.
The reason it worked with gcc is probably different warning/error level settings. It is possible to compile the code without #including the header and having the declaration of strcmp. The compiler will not be able to do type checking but the symbol still gets resolved by the linker.
You can also avoid using strcmp completely and write
#include <string>
#include <iostream>
int main (int argc, char *argv[]) {
std::string command = argv[0];
if( command != "./test" ) {
std::cout << "not equal" << endl;
} else {
std::cout << "equal" << endl;
}
return 0;
}
Using a std::string on one side of the comparison will cause the "./test" string to be converted into a std::string as well and the comparison will be done by the == operator of the std::string class.
You're not including the correct header file
#include <cstring>
You need to #include <cstring> (or possibly #include <string.h>.)
Many compilers include extra standard headers when you include another. The Standard allows this; it's your responsibility to use the headers that guarantee declarations for what you use, not just headers that happen to have the declarations for your compiler.
You have to include <cstring>. <string> is the header for C++ strings.
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.