简体   繁体   English

getline C没有匹配功能

[英]no matching function for getline c++

I'm trying to input a number, and based on that number, the user would have to enter x amount of times. 我正在尝试输入一个数字,并且基于该数字,用户将不得不输入x次。

For example 例如

3 //how many the user wants
192 231 2 3
22192 2 1 23
2831 3 23 1

I tried doing this, but it keeps saying no matching function for getline 我尝试这样做,但一直说getline没有匹配功能

int* x = NULL;
int numbers;
cin >> numbers;
x = new int[numbers]

for (int i=0;i<numbers;i++)
{
    std::getline(std::cin, numbers)
    x[i] = numbers
}

getline的第二个参数的类型为std::string

You definitely do not want to use std::getline as it doesn't look like you want a string of numbers, but rather numbers themselves. 您绝对不希望使用std::getline因为它看起来好像不需要数字字符串,而是数字本身。

What you want is to read number by number, so use the same thing you did to read in the numbers , but do not read it in numbers again. 您想要的是按数字读取数字,因此请使用与读取numbers ,但不要再次按numbers读取它。 (Because you are using it in the loop.) (因为您正在循环中使用它。)

Anyway, an approximation of what you want is this: 无论如何,这是您想要的近似值:

int how_many;
std::vector<int> numbers;
std::cin >> how_many;
for (int i = 0; i < how_many; i++){
    int temp;
    std::cin >> temp;
    numbers.push_back(temp);
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM