对于函数getline C无匹配函数++ [英] no matching function for getline c++

查看:216
本文介绍了对于函数getline C无匹配函数++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图输入一个号码,并根据该数,用户将不得不进入次数X量

例如

  3 //用户多少希望
192 231 2 3
22192 2 1 23
2831 3 23 1

我试着这样做,但它口口声声说因为没有匹配的函数函数getline

 为int * X = NULL;
INT数;
CIN>>数;
X =新的INT [数字]的for(int i = 0; I<数字;我++)
{
    的std ::函数getline(给std :: cin,数字)
    X [i] =号码
}


解决方案

您肯定不希望使用的std ::函数getline ,因为它看起来并不像你想一串数字,而是数字本身。

您需要的是通过数字阅读次数,所以用你没有在数字读同样的事情,但不要在数字一次。 (因为你正在使用它的循环。)

总之,你想要什么的近似值是这样的:

  INT HOW_MANY;
的std ::矢量<&INT GT;数;
给std :: cin>>多少;
的for(int i = 0; I< HOW_MANY;我++){
    INT温度;
    给std :: cin>>温度;
    numbers.push_back(临时);
}

I'm trying to input a number, and based on that number, the user would have to enter x amount of times.

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

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
}

解决方案

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.

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. (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);
}

这篇关于对于函数getline C无匹配函数++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆