Getline错误 - “无法推断模板参数”

时间:2012-02-16 07:35:02

标签: 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;

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'

2 个答案:

答案 0 :(得分:2)

此处的问题是getline需要std::string(或其他类型的basic_string)作为其参数,并且您提供int。在我mentioned in my answer to your previous question时,您应该将movieName的类型切换为std::string。您还应该适当地更改其他变量的类型(如果您将其他值乘以float s,它们应该是float s),我建议将定义移到后面的main实际使用它们。

希望这有帮助!

答案 1 :(得分:0)

我看到的第一件事是movieName被声明为整数而不是字符串或char数组(C字符串)。这似乎是导致getline出现多个错误的主要问题。