gpt4 book ai didi

c++ - 无法从大括号括起来的初始值设定项列表转换为结构

转载 作者:行者123 更新时间:2023-11-30 02:22:50 27 4
gpt4 key购买 nike

我环顾四周,找不到可以清楚地回答我的问题的问题,所以我发布了这个问题。当我尝试编译我的代码时出现此错误:

welcomebits.cpp:30:74: error: could not convert ‘{tmp_fullname, tmp_user_name, tmp_PIN, tmp_balance}’ from ‘<brace-enclosed initializer list>’ to ‘User’
User u={tmp_fullname, tmp_user_name, tmp_PIN, tmp_balance};
^

为什么会这样?我是 c++ 的新手,我只是习惯了结构,类和对象仍然超出了我的范围。我最近学习了通过引用和指针传递,所以我在基本意义上理解了这些。

这是我的结构定义和发生错误的函数:

struct User{
std::string fullname="";
std::string user_name="";
float PIN=0.;
float balance=0.;
};


void create_user_data(std::vector<User>& uv){

std::ifstream reader;
std::string holder="";
char comma=',';
std::string tmp_fullname="";
std::string tmp_user_name="";
float tmp_PIN=0.;
float tmp_balance=0.;

reader.open("database.csv");

while(std::getline(reader, holder)){
std::istringstream ss(holder);
std::getline(ss, tmp_fullname, ',');
std::getline(ss, tmp_user_name, ',');
ss>>tmp_PIN>>comma;
ss>>tmp_balance;

User u={tmp_fullname, tmp_user_name, tmp_PIN, tmp_balance};
uv.push_back(u);
}
}

感谢您花时间帮助大家。

最佳答案

您需要在启用 C++14 支持的情况下进行编译。这在 C++11 和更早的标准中不受支持。 C++14 允许 aggregate initialization在具有成员初始值设定项的类/结构上。如果您删除了默认成员初始值设定项:

struct User{
std::string fullname;
std::string user_name;
float PIN;
float balance;
};

然后您的代码将使用 C++11 进行编译。

关于c++ - 无法从大括号括起来的初始值设定项列表转换为结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46858550/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com