【问题标题】:c++ error, "could not deduce argument for 'std..."c ++错误,“无法推断'std ...”的参数
【发布时间】:2010-12-03 01:04:05
【问题描述】:

我的程序总是遇到同样的错误,我们将不胜感激。

错误 1 ​​错误 C2784: 'std::basic_istream<_elem> &std::getline(std::basic_istream<_elem> &,std::basic_string<_elem> &)' : 无法推断 'std::basic_string<_elem> &' 的模板参数来自 'char' c:\Users\esmier\Documents\Visual Studio 2008\Projects\Chapter 4 lab\Chapter 4 lab\source.cpp 40 Chapter 4 lab

错误 2 错误 C2780: 'std::basic_istream<_elem> &std::getline(std::basic_istream<_elem> &,std::basic_string<_elem> &,const _Elem)' : 期望 3 参数 - 2 个提供 c:\Users\esmier\Documents\Visual Studio 2008\Projects\Chapter 4 lab\Chapter 4 lab\source.cpp 40 Chapter 4 lab

注释为 case b 的部分也会发生错误

#include <iostream>
#include<sstream>
int main()
{
double hours;
double Package_A_price=9.95, Package_A_hours=10, Package_A_additional=2.00,Package_A_total;
double Package_B_price=14.95, Package_B_hours=20, Package_B_additional=1.00,Package_B_total;
double Package_C_price=19.95;
char package_choice, additional_hours[3];

//table output

cout<<"Please choose your package below";

cout<<"Package A:\t"<<"For $9.95 per month 10 hours of service are provided.\n";
cout<<"\t\tAdditional hours are $2.00 per hour\n\n";

cout<<"Package B:\t"<<"For $14.95 per month 20 hours of service are provided.\n";
cout<<"\t\tAdditional hours are $1.00 per hour\n\n";

cout<<"Package A:\t"<<"For $19.95 per month unlimited access is provided.\n\n\n";

cout<<"What is your package letter?\n";
cin>>package_choice;
while (package_choice!='A'||'a'||'B'||'b'||'C'||'c')
{
    cout<<"You entered an incorrect option.\n";
    cout<<"please reenter your choice.\n\n";
}
switch (package_choice)
{
//package A choice
case 'A||a':
    cout<<"Did you have any additional hours?\n";
    getline(cin, additional_hours);
    if (additional_hours == 'yes')
    {
        cout<<"How many hours did you have?\n";
        cin>>hours;
        while (hours >744)
        {
            cout<<"You can not have over 744 hours per month!\n";
            cout<<"Time is a linear constant, Please renter your amount.\n";
        }
        Package_A_total=9.95+(2.00*hours);
        cout<<"Your Total for Package A is $"<<Package_A_total;
        break;
    }
//package B choice
case 'B||b':
    cout<<"Did you have any additional hours?\n";
    getline(cin, additional_hours);
    if (additional_hours == 'yes')
    {
        cout<<"How many hours did you have?\n";
        cin>>hours;
        while (hours >744)
        {
            cout<<"You can not have over 744 hours per month!\n";
            cout<<"Time is a linear constant, Please renter your amount.\n";
        }
        Package_B_total=14.95+(1.00*hours);
        cout<<"Your Total for Package B is $"<<Package_B_total;
        break;
    }
//package C choice
case 'C||c':
    cout<<"Your Total for Package C is $"<<Package_B_price;
    break;

default: cout<<"You did not Enter A B or C.\n";
         cout<<"Please reenter your choice.";
}



system ("pause");
    return 0;

}

【问题讨论】:

    标签: c++


    【解决方案1】:

    additional_hours 被声明为 a character 3 个字符的数组。这种形式的 getline 要求您使用 std::string(另一种 C 风格的形式 char* 是 cin 的成员函数)。

    还有许多其他错误,显然是因为您不知道 char 表示单个字符:

    case 'A||a':
    if (additional_hours == 'yes')
    

    那些使用多字节字符常量。基本上它根本不是你所期望的(它是一个用 ASCII 字符或类似字符编码的单个数字)。

    while (package_choice!='A','a','B','b','C','c')
    

    同样,这不是您比较多个条件的方式,您需要将其与逻辑与和或(&& 和 ||)结合起来。实际上,它使用逗号运算符,这意味着评估所有操作数并使用最后一个 ('c') 作为结果。

    另外,&lt;cctype&gt; 中的 tolower 或 toupper 函数可能会对您有所帮助。

    打开编译器警告(-Wall with GCC)可能会显示所有这些错误:[警告]多字符字符常量,[警告]逗号左侧操作数无效,[警告]案例标签值超过最大值对于类型,由于数据类型的范围有限,[警告] 比较总是错误的。

    编辑:既然您已将 additional_hours 声明为一个字符数组,您应该知道 3 个字符不足以存储字符串“yes”,因为空终止符没有空间。你最好使用 std::string 类。

    【讨论】:

    • 如果您查看变量描述,额外的小时数被定义为 3 个字母
    • 修复了这个问题。但是,asker 编辑了原代码:char package_choice, additional_hours(3);
    【解决方案2】:

    我已经看到一个缺陷,您的 while 循环没有重置 package_choice:

    while (package_choice!='A','a','B','b','C','c'){       
             cout<<"You entered an incorrect option.\n";        
             cout<<"please reenter your choice.\n\n";
    }
    

    您需要再次输入 cin >> 包选择。

    【讨论】:

    • 在while循环中变量可以在语句之前
    【解决方案3】:

    试试这个:

    cin.getline(additional_hour, 3)
    

    【讨论】:

      【解决方案4】:

      使用std::coutstd::cinstd::cin.getline(..) 而不是您正在使用的。

      使用std::cin.get() 而不是system("pause") 来固定屏幕

      【讨论】:

      • 或简单地添加“使用命名空间标准;”在你的 main 函数之前,因为标准 C++ 库的所有全局标识符都在命名空间“std”中定义
      • 是的,对不起,使用命名空间标准;是不是我复制失败了
      【解决方案5】:

      它似乎不喜欢你传递给 getline 的内容。尝试将 additional_hours 声明为 std::string

      【讨论】:

        猜你喜欢
        • 2011-08-20
        • 2010-12-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-10-17
        • 1970-01-01
        • 2012-11-14
        • 1970-01-01
        相关资源
        最近更新 更多