C++ 错误, "could not deduce argument for ' 标准...”

标签 c++

我的程序不断出现同样的错误,我们将不胜感激。

Error 1 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 'char' c:\Users\esmier\Documents\Visual Studio 2008\Projects\Chapter 4 lab\Chapter 4 lab\source.cpp 40 Chapter 4 lab

Error 2 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 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;

最佳答案

additional_hours被声明为 一个字符 一个包含 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') 作为结果。

此外,<cctype> 中的 tolower 或 toupper 函数可能对你有帮助。

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

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

关于C++ 错误, "could not deduce argument for ' 标准...”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1504674/

相关文章:

c++ - 在 C++ 中更有效和快速的反转矩阵的方法(大和小)

c++ - 如何在 c-ares 中获取 DNS 服务器

c++ - 为什么 c 位域中的 unsigned int 值变成了 signed 值?

c++ - 开关盒标签上的 PC-lint : Violates MISRA C++ 2008 Required Rule 5-0-12

c++ - 超速 vector push_back

c++ - "classes are not objects"是什么意思?

c++ - CRT 9.0 vsprintf_s 对长格式字符串的参数验证

c++ - 在 QML 中使用 C++ 枚举作为字符串

c++ - C++11和C++14如何实现动态函数调用?

c++ - 如何最好地为 C++11 中的 unique_ptr 中的普通数组制作迭代器?