关于c ++中struct的问题

dat*_*ili -1 c++

我有以下代码

#include <iostream>
#include<string>
#include <sstream>
using namespace std;
struct product{
    int weight;
    float price;
};
int main(){
    string  mystr;
    product  prod;
    product *pointer;
    pointer=&prod;
    getline(cin,pointer->price);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

但它告诉我错误

没有重载函数的实例"getline"匹配参数列表

这是什么错误?

Tyl*_*nry 13

错误在于您正在尝试将一行文本读入float.阅读整行需要您读入一个字符串.如果你只想从输入读取一个浮点数,只需写:

cin >> pointer->price;
Run Code Online (Sandbox Code Playgroud)

这将读取输入到下一个空格并尝试将其解释为float.