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.
Related
Can someone tell me how I can make use of std::map.operator[] to get the value of a const std::map ?
For example:
file.h
#ifndef _FILE_H
#define _FILE_H
#include <map>
#include <string>
const std::map<std::string,std::string> STRINGS= {
{"COMPANY","MyCo"}
,{"YEAR","2022"}
};
#endif
file.cpp
#include "cpp_playground.h"
#include <iostream>
int main (void){
std::cout<< "Code by "<< STRINGS["COMPANY"] << " " << STRINGS["YEAR"] << std::endl;
}
With Visual Studio 2015 I get this error, but I can't interpret it
Severity Code Description Project File Line Suppression State
Error C2678 binary '[': no operator found which takes a left-hand operand of type 'const std::map<std::string,std::string,std::less<_Kty>,std::allocator<std::pair<const _Kty,_Ty>>>' (or there is no acceptable conversion) cpp_playground d:\user\documents\visual studio 2015\projects\cpp_playground\cpp_playground.cpp 10
The operator[] in std::map is defined to return a reference to the object with the given key - or create it, if it doesn't exist, which modifies the map, that's why it's not a const method. There is no const version of that operator, that's why you get the shown error.
Use std::map's at(...) function for access-only. Note that it throws a std::out_of_range exception if the given key is not contained in the map; in C++20 or later you can use contains() to check whether the given key exists.
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.
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<<.
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.
This code works on Code Blocks but not on Visual Studio:
// A simple program that prints string test1:
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<string> test1 = { "pooping","reading" };
for (vector<string>::iterator iter = test1.begin(); iter != test1.end(); iter++)
{
cout << *iter << endl;
}
system("pause");
}
The error that Visual Studio Outputs:
Error C2679 binary '<<': no operator found which takes a right-hand operand of type 'std::_Vector_iterator<std::_Vector_val<std::_Simple_types<std::basic_string<char,std::char_traits<char>,std::allocator<char>>>>>' (or there is no acceptable conversion)
std::basic_string is officially defined in the <string> header. See cppreference.
#include<string>
The class template basic_string stores and manipulates sequences of char-like objects. The class is dependent neither on the character type nor on the nature of operations on that type. The definitions of the operations are supplied via the Traits template parameter - a specialization of std::char_traits or a compatible traits class. For more details please see
std::basic_string
after adding header , program won't compile and it will throw error
"error: in C++98 'test1' must be initialized by constructor, not by '{...}'"
check the below program followed another method to initialize vector.
As suggested by other you need to add
#include<string>
// A simple program that prints string test1:
#include <iostream>
#include <vector>
#include<string>
using namespace std;
int main()
{
static const string arr[] = {"pooping","reading"};
vector<string> test1 (arr, arr + sizeof(arr) / sizeof(arr[0]) );
for (vector<string>::iterator iter = test1.begin(); iter != test1.end(); iter++)
{
cout << *iter << endl;
}
system("pause");
}