简体   繁体   English

当使用带有int的getline时,如何修复“没有重载函数的实例”getline“匹配参数列表”

[英]How to fix “no instance of overloaded function ”getline“ matches the argument list” when using getline with an int

Im currently working on an assignment and am trying to use try catch error handling for checking a user's input is a valid int. 我目前正在进行一项任务,我正在尝试使用try catch错误处理来检查用户的输入是否是有效的int。

I currently have this: 我目前有这个:

int inputValidation() {
    int e = 0;
    std::string es;
    bool check = false;
    do {
        try {
            if (!getline(std::cin, e)) {
                throw stringInput;
            }
            else {
                check = true;
            }
        }
        catch (std::exception& er) {
            std::cout << "Error! " << er.what() << std::endl;
            std::cin.clear();
            std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
        }
    } while (!check);
    return e;
}

my issue is with the if ((getline(std::cin, e))){} part. 我的问题是if ((getline(std::cin, e))){}部分。 I've also tried using std::cin.getline(e, 256) 我也尝试过使用std::cin.getline(e, 256)

When calling the function I'm using this loop: 调用函数时我正在使用这个循环:

do {
        std::cout << "Please select a month: ";
        selectedMonth = inputValidation();
    } while (selectedMonth < 1 || selectedMonth >(12 - actualMonth));

This simply makes sure they can only input a month from the current month until December. 这只是确保他们只能输入从当月到12月的一个月。

I'm aware that I can use es instead of e , however that defeats the purpose of the error checking. 我知道我可以使用es而不是e ,但这会破坏错误检查的目的。 My only idea coming off of this is to check on a conversion. 我唯一的想法是检查转换。

For whatever reason I seem to be getting the error "no instance of overloaded function "getline"" and am unsure of where I'm going wrong. 无论出于何种原因,我似乎得到错误“没有重载函数的实例”getline“”并且我不确定我哪里出错了。 If anyone could provide some insight I would be very grateful. 如果有人能提供一些见解,我将非常感激。

if std::cin >> e is not appropriate, you could use istringstream : 如果std::cin >> e不合适,可以使用istringstream

std::string asText;

std::getline(cin,asText);

std::istringstream iss (asText);

if (iss >> e)

First Version 第一版

I managed to fix this by changing it to: 我设法通过将其更改为:

int inputValidation(std::string message) {
    int e = NULL;
    std::string es;
    bool check = false;
    do {
        try {
            std::cout << message;
            getline(std::cin, es);
            if (!atoi(es.c_str())) {
                throw stringInput;

            }
            else {
                e = atoi(es.c_str());
                check = true;
            }
        }
        catch (std::exception& er) {
            std::cout << "Error! " << er.what() << std::endl;
        }
    } while (!check);
    return e;
}
//In another function -->
    do {
        selectedMonth = inputValidation("Please select a month: ");
    } while (selectedMonth < 1 || selectedMonth >(12 - actualMonth));

Working Version 工作版

As of the comments I changed this. 截至评论我改变了这一点。

(The only difference is this version doesn't include an exception) (唯一的区别是这个版本不包括例外)

bool checkInput(int &input, std::string message) {
    try {
        std::cin >> input;
        if (!std::cin.fail()) {
            return true;
        }
        else {
            throw(message);
        }
    }
    catch (std::string e) {
        std::cout << "Invalid input!" << std::endl;
        std::cout << e << std::endl;
        std::cin.clear();
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
        return false;
    }
}

//Elsewhere -->
std::cout << "Please input the lowest number you would like to check" << std::endl;
while (!checkInput(lowestNumber, "Please input the lowest number you would like to check"));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 没有重载函数的实例“getline”匹配参数列表 - no instance of overloaded function “getline” matches argument list getline在C ++中使用字符串返回错误:没有重载函数getline的实例与参数列表匹配 - getline using strings in C++ returning error: No instance of overloaded function getline matches the argument list C ++没有重载函数getline的实例匹配参数列表 - C++ No instance of overloaded function getline matches argument list 错误没有重载的实例 function “getline” 与参数列表匹配 - Error No instance of overloaded function “getline” matches the argument list 错误没有重载函数的实例“getline”匹配参数列表c ++ - error no instance of overloaded function “getline” matches the argument list c++ C++ 'getline' 没有重载的实例 function 匹配参数列表 - C++ 'getline' No instance of overloaded function matches argument list 将GetLine与ifstream一起使用-没有“ getline”的实例与参数列表匹配 - Using GetLine with ifstream - no instance of “getline” matches the argument list 没有重载的实例 function “getline”匹配参数列表——参数类型是:(std::ifstream,char) - no instance of overloaded function "getline" matches the argument list -- argument types are: (std::ifstream,char) 错误:没有重载 function “std::getline”实例与参数列表匹配 — 参数类型为:(std::istream, char [50]) - Error: No instance of overloaded function “std::getline” matches the argument list — argument types are: (std::istream, char [50]) 为什么我收到错误:没有重载的 function “getline”实例与此处的参数列表匹配? - why am I getting a error: no instance of an overloaded function "getline" matches the argument list here?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM