【问题标题】:How to fix "no instance of overloaded function "getline" matches the argument list" when using getline with an int使用带有int的getline时如何修复“没有重载函数“getline”的实例与参数列表匹配”
【发布时间】:2019-04-23 12:23:29
【问题描述】:

我目前正在处理一项任务,并尝试使用 try catch 错误处理来检查用户的输入是否是有效的 int。

我目前有这个:

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

我的问题是 if ((getline(std::cin, e))){} 部分。我也试过使用std::cin.getline(e, 256)

调用函数时我使用的是这个循环:

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

这只是确保他们只能输入从当前月份到 12 月的月份。

我知道我可以使用es 而不是e,但这违背了错误检查的目的。我唯一的想法是检查转化。

无论出于何种原因,我似乎收到错误“没有重载函数“getline”的实例”,并且不确定我哪里出错了。如果有人能提供一些见解,我将非常感激。

【问题讨论】:

  • 您可以通过使用正确的参数来修复它,getline always takes a string,而不是 int。
  • std::cin &gt;&gt; e; 有什么问题?
  • @Borgleader 谢谢 :) 有没有办法在 if 语句之前使用该输入,然后检查转换?再次感谢
  • @mch 我正在努力清除缓冲区,发现尽管输入有效,但循环永远不会中断:/
  • @mch 我认为这是我的循环的问题——我的错

标签: c++


【解决方案1】:

如果std::cin &gt;&gt; e不合适,你可以使用istringstream

std::string asText;

std::getline(cin,asText);

std::istringstream iss (asText);

if (iss >> e)

【讨论】:

  • 感谢贾斯珀的留言!我真的只是解决了这个问题,然后看到了你的回复:/
【解决方案2】:

第一版

我设法将其更改为:

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));

工作版

从 cmets 开始,我改变了这个。

(唯一的区别是这个版本不包含异常)

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"));

【讨论】:

  • 那行不通。 atoi() 返回转换后的 int,而不是表示成功的 bool。仅当解析的 int 为 0 时才会指示失败。读取不可解析的字符串会导致未定义的行为。使用 istringstream 或 strtol。 cplusplus.com/reference/cstdlib/atoi
  • @JasperKent 我明白你的意思了。但是由于某种原因,它对我有用,明天的任务就到期了。我不想改变任何东西,除非我知道我提前了。我不想在这里听起来像一个**,只是有时间压力。提交后我会去看看它,因为我正在尝试使用 C++ 做得更好。谢谢你的回复:)
  • 它可能看起来有效,但相信我 - 它不是。
  • 好的,我会把它改成你的版本-谢谢你的帮助:)
  • 我刚刚更改了它,现在 if 语句每次都运行,尽管得到了有效的输入。 std::string asText; getline(std::cin, asText); std::istringstream iss(asText); if (!iss >> e) { throw stringInput; }
猜你喜欢
  • 2023-03-10
  • 2013-11-17
  • 1970-01-01
  • 1970-01-01
  • 2014-12-27
  • 1970-01-01
  • 2021-09-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多