0
我环顾四周,无法找到明确回答我的问题的问题,因此我发布了此问题。 我得到这个错误,当我尝试编译我的代码:无法转换为大括号包含的初始化程序列表中的结构
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);
}
}
感谢您的时间和大家有所帮助。
无法用MinGW g ++和Visual C++重现。你正在使用哪种编译器?请使用所有选项发布确切的调用。 –
你使用C++ 11吗? – scohe001
如果您发布了**完整的**,但可以重现问题的最小示例,则可能会有所帮助。 –