英文:
make_pair:error C2665: no overloaded function could convert all the argument types
问题
#include <vector>
#include <utility>
#include <string>
#include <iostream>
using namespace std;
int main()
{
vector<pair<string, int>> v;
string word;
int i; // Changed 'i' to 'int'
while (cin >> word >> i) // Changed 'i' to 'int'
v.push_back(make_pair(word, i));
return 0;
}
It reports:
error C2665: 'std::vector<std::pair<std::string, int>,std::allocator<std::pair<std::string, int>>>::push_back': no overloaded function could convert all the argument types
Can someone tell me where the problem is?
英文:
An exercise on C++ primer:Write a program to read a sequence of strings and ints,storing each into a pair.Store the pairs in a vector.
#include <vector>
#include <utility>
#include <string>
#include <iostream>
using namespace std;
int main()
{
vector<pair<string, int>> v;
string word, i;
while (cin >> word >> i)
v.push_back(make_pair( word, i ));
return 0;
}
It reports:
> error C2665:
> 'std::vector<std::pair<std::string,int>,std::allocator<std::pair<std::string,int>>>::push_back':
> no overloaded function could convert all the argument types
Can someone tell me where the problem is?
答案1
得分: 4
You define the variable i
as a std::string
, when you're supposed to define it as an int
:
std::string word;
int i;
With the current code, the call
make_pair( word, i )
is the same as
make_pair<std::string, std::string>( word, i )
There's no automatic conversion anywhere from std::pair<std::string, std::string>
(created by your make_pair
call) to a std::pair<std::string, int>
. The two types are just not the same and not compatible in any way.
英文:
string word, i;
You define the variable i
as a std::string
, when you're supposed to define it as an int
:
std::string word;
int i;
With the current code, the call
make_pair( word, i )
is the same as
make_pair<std::string, std::string>( word, i )
There's no automatic conversion anywhere from std::pair<std::string, std::string>
(create by your make_pair
call) to a std::pair<std::string, int>
. The two types are just not the same and not compatible in any way.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论