C++ Getline错误--";无法推断模板参数";
这是一个非常简单的程序,我不知道我做错了什么。我在网上查过了,没有找到任何有用的东西。我的getline(cin,movieName)有点问题,但我不知道是什么C++ Getline错误--";无法推断模板参数";,c++,string,compiler-errors,getline,C++,String,Compiler Errors,Getline,这是一个非常简单的程序,我不知道我做错了什么。我在网上查过了,没有找到任何有用的东西。我的getline(cin,movieName)有点问题,但我不知道是什么 //This program will calculate a theater's revenue from a specific movie. #include<iostream> #include<iomanip> #include<string> using namespace std; in
//This program will calculate a theater's revenue from a specific movie.
#include<iostream>
#include<iomanip>
#include<string>
using namespace std;
int main ()
{
const float APRICE = 6.00,
CPRICE = 3.00;
int movieName,
aSold,
cSold,
gRev,
nRev,
dFee;
cout << "Movie title: ";
getline(cin, movieName);
cout << "Adult tickets sold: ";
cin.ignore();
cin >> aSold;
cout << "Child tickets sold: ";
cin >> cSold;
gRev = (aSold * APRICE) + (cSold * CPRICE);
nRev = gRev/5.0;
dFee = gRev - nRev;
cout << fixed << showpoint << setprecision(2);
cout << "Movie title:" << setw(48) << movieName << endl;
cout << "Number of adult tickets sold:" << setw(31) << aSold << endl;
cout << "Number of child tickets sold:" <<setw(31) << cSold << endl;
cout << "Gross revenue:" << setw(36) << "$" << setw(10) << gRev << endl;
cout << "Distributor fee:" << setw(34) << "$" << setw(10) << dFee << endl;
cout << "Net revenue:" << setw(38) << "$" << setw(10) << nRev << endl;
return 0;
}
//此程序将计算影院从特定电影中获得的收入。
#包括
#包括
#包括
使用名称空间std;
int main()
{
常量浮点APRICE=6.00,
CPRICE=3.00;
int movieName,
阿索尔德,
cSold,
格雷夫,
nRev,
dFee;
coutasold;
cout>cSold;
gRev=(aSold*APRICE)+(cSold*CPRICE);
nRev=gRev/5.0;
dFee=gRev-nRev;
cout这里的问题是getline
需要std::string
(或者其他类型的basic_string
)作为它的参数,您提供了一个int
。与我一样,您应该将movieName
的类型切换为std::string
。您还应该适当地更改其他变量的类型(如果将其他值乘以float
s,那么它们可能应该是float
s),我建议将定义移到后面的main
中实际使用的位置
希望这有帮助!我看到的第一件事是movieName被声明为整数,而不是字符串或字符数组(C字符串)。这似乎是导致getline出现多个错误的主要问题
error 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' see declaration of 'std::getline'
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
see declaration of 'std::getline'
error 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'
see declaration of 'std::getline'
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
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\string(395) : see declaration of 'std::getline'