这是我的第一个项目,我不明白我得到的错误
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
int Multiple1 = 0;
int Multiple2 = 0;
cout << "Input your first Multiple: " << endl;
getline(cin, Multiple1);
cout <<"Your First Multiple is: " << Multiple1 << endl;
cout << "Input your second Multiple: " << endl;
getline(cin, Multiple2);
cout << "Your Second Multiple is: " << Multiple2 << endl;
for (int i = 1; i <= 100; i++)
{
if (i % Multiple1 == 0 && i % Multiple2 == 0)
cout << "FizzBuzz" << endl;
else
if (i % Multiple1 == 0)
cout << "Fizz" << endl;
else
if (i % Multiple2 == 0)
cout << "Buzz" << endl;
else cout << i << endl;
}
cin.get();
return 0;
}
以下是错误:
运行代码时,为什么getline(cin, Multiple1);
和getline(cin, Multiple2);
不起作用?
错误代码
E0304: E0304 no instance of overloaded function "getline" matches the argument list E0304: no instance of overloaded function "getline" matches the argument list C2672: 'getline': no matching overloaded function found C2784:'std::basic_istream<_Elem,_Traits> &std::getline(std::basic_istream<_Elem,_Traits> &,std::basic_string<_Elem,_Traits,_Alloc> &)': could not deduce template argument for 'std::basic_string<_Elem,_Traits,_Alloc> &' from 'int' C2780: 'std::basic_istream<_Elem,_Traits> &std::getline(std::basic_istream<_Elem,_Traits> &,std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem)': expects 3 arguments - 2 provided C2784: 'std::basic_istream<_Elem,_Traits> &std::getline(std::basic_istream<_Elem,_Traits> &,std::basic_string<_Elem,_Traits,_Alloc> &)': could not deduce template argument for 'std::basic_string<_Elem,_Traits,_Alloc> &' from 'int' C2780: 'std::basic_istream<_Elem,_Traits> &std::getline(std::basic_istream<_Elem,_Traits> &,std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem)': expects 3 arguments - 2 provided C2784: 'std::basic_istream<_Elem,_Traits> &std::getline(std::basic_istream<_Elem,_Traits> &,std::basic_string<_Elem,_Traits,_Alloc> &)': could not deduce template argument for 'std::basic_string<_Elem,_Traits,_Alloc> &' from 'int' C2780: Severity Code Description Project File Line Suppression State Error C2780 'std::basic_istream<_Elem,_Traits> &std::getline(std::basic_istream<_Elem,_Traits> &&,std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem)': expects 3 arguments - 2 provided
答案 0 :(得分:0)
正如评论中已有人指出的那样,getline
具有可以采取的具体参数。第二个参数必须是string
类型,但您尝试传入int
。这就是您遇到错误的原因,因为编译器期望参数具有特定类型。
根据您的目的,有很多方法可以从用户那里获得输入。查看您提供的代码,您似乎只想从用户那里获得int
。在这种情况下,getline
似乎不合适,因为它会从输入流中获取字符并将它们放入string
。 cin
本身就足够了:
int n;
cin >> n;
答案 1 :(得分:0)
因此,在这种情况下,您正在使用std::getline()
函数错误。 std::getline()
的目的是从标准输入中捕获数据,然后将此数据分配给字符串类型的变量。
在您的代码中,您将getline()
一个整数变量或 int type 解析为该函数,并且编译器因此而产生错误。
您的代码有两个可以修复的修复程序。一种是修复代码的最佳方法,另一种是由于某种原因绝对必须使用getline()
。
要在不使用getline()
的情况下修复代码,我建议您直接使用>>
运算符从标准输入到变量,就像这样;
int main()
{
int Multiple1 = 0;
int Multiple2 = 0;
//First Multiple is taken in
cout << "Input your first Multiple: " << endl;
cin >> Multiple1;
cout <<"Your First Multiple is: " << Multiple1 << endl;
//Second Multiple is taken in
cout << "Input your second Multiple: " << endl;
cin >> Multiple2;
cout << "Your Second Multiple is: " << Multiple2 << endl;
或者,如果绝对必须使用getline()
,则只要字符串需要在数学或整数相关函数中使用,就可以从字符串转换为整数,尽管在这种情况下这是不好的做法。注意我已经改变了Multiple1&amp; Multiple2到字符串变量,并使用stoi()
函数将字符串更改为整数值,只要字符串用于与整数进行比较。此解决方案适用于c ++ 11或更高版本。
int main()
{
string Multiple1;
string Multiple2;
//First Multiple is taken in
cout << "Input your first Multiple: " << endl;
getline(cin,Multiple1);
cout <<"Your First Multiple is: " << Multiple1 << endl;
//Second Multiple is taken in
cout << "Input your second Multiple: " << endl;
getline(cin,Multiple2);
cout << "Your Second Multiple is: " << Multiple2 << endl;
for (int i = 1; i <= 100; i++)
{
if (i % stoi(Multiple1) == 0 && i % stoi(Multiple2) == 0){
cout << "FizzBuzz" << endl;
}
else{
if (i % stoi(Multiple1) == 0){
cout << "Fizz" << endl;
}
else{
if (i % stoi(Multiple2) == 0){
cout << "Buzz" << endl;
}
else{
cout << i << endl;
}
}
}
}
cin.get();
return 0;
}
希望这一切都能澄清您的问题。顺便说一句,使用基于bash的本机编译器(如g ++)可以使您的学习体验更容易一些,因为错误可以更容易理解和谷歌。