gpt4 book ai didi

c++ - Getline 错误 - -"could not deduce template argument"

转载 作者:行者123 更新时间:2023-11-30 04:29:30 26 4
gpt4 key购买 nike

这是一个非常简单的程序,我不知道我做错了什么。我在网上看过,但找不到任何有用的东西。我的 getline(cin, movieName) 有问题,但我不知道是什么。

//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;
}

这是我遇到的错误:

 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'

最佳答案

这里的问题是 getline 需要一个 std::string(或一些其他类型的 basic_string)作为它的参数,而你正在提供一个 int。正如我mentioned in my answer to your previous question ,您应该将 movieName 的类型切换为 std::string。您还应该适本地更改其他变量的类型(如果您将其他值乘以 float,它们可能应该是 float),我建议移动定义一直到 main 中实际使用它们的地方。

希望这对您有所帮助!

关于c++ - Getline 错误 - -"could not deduce template argument",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9307248/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com