没有匹配的函数可供调用
我得到了这种方法,我从文件中获取用户数据行,然后将这些行分成几个部分,然后将这些部分放入不同的字符串中。最后,我创建一个新的用户对象并将该用户对象添加到另一个名为 users 的向量中。
void userdata::importData(string fname)
{
g_string g;
filename= fname;
string line;
ifstream file(filename.c_str());
vector<string> parts;
while ( file.good() )
{
getline (file,line);
g.split(',', line);
vector<string>::iterator i;
for(i = parts.begin(); i < parts.end(); i++)
{
string usernr = parts.at(0);
string name = parts.at(1);
string gender = parts.at(2);
string age = parts.at(3);
string profession = parts.at(4);
string email = parts.at(5);
user user(usernr , name , gender , age , profession , email );
users.push_back(user);
}
parts.clear();
}
file.close();
}
问题是,我在将字符串设置为 parts.at(int)-value 的每一行上都会遇到此错误:
test.cpp: In method `void userdata::importData(class string)':
test.cpp:186: no matching function for call to `vector<basic_string<char
,string_char_traits<char>,__default_alloc_template<false,0> >,__default_alloc_te
mplate<false,0> >::at (int)'
test.cpp:187: no matching function for call to `vector<basic_string<char
,string_char_traits<char>,__default_alloc_template<false,0> >,__default_alloc_te
mplate<false,0> >::at (int)'
test.cpp:188: no matching function for call to `vector<basic_string<char
,string_char_traits<char>,__default_alloc_template<false,0> >,__default_alloc_te
mplate<false,0> >::at (int)'
test.cpp:189: no matching function for call to `vector<basic_string<char
,string_char_traits<char>,__default_alloc_template<false,0> >,__default_alloc_te
mplate<false,0> >::at (int)'
test.cpp:190: no matching function for call to `vector<basic_string<char
,string_char_traits<char>,__default_alloc_template<false,0> >,__default_alloc_te
mplate<false,0> >::at (int)'
test.cpp:191: no matching function for call to `vector<basic_string<char
,string_char_traits<char>,__default_alloc_template<false,0> >,__default_alloc_te
mplate<false,0> >::at (int)'
有人知道可能出了什么问题吗?
I got this method where I'm getting lines of userdata from a file, then splitting the lines into parts and then putting those parts into different strings. Then at last, I'm creating a new user object and adding the user object into another vector called users.
void userdata::importData(string fname)
{
g_string g;
filename= fname;
string line;
ifstream file(filename.c_str());
vector<string> parts;
while ( file.good() )
{
getline (file,line);
g.split(',', line);
vector<string>::iterator i;
for(i = parts.begin(); i < parts.end(); i++)
{
string usernr = parts.at(0);
string name = parts.at(1);
string gender = parts.at(2);
string age = parts.at(3);
string profession = parts.at(4);
string email = parts.at(5);
user user(usernr , name , gender , age , profession , email );
users.push_back(user);
}
parts.clear();
}
file.close();
}
The problem is that I'm getting this error on every line where I'm setting the string as the parts.at(int)-value:
test.cpp: In method `void userdata::importData(class string)':
test.cpp:186: no matching function for call to `vector<basic_string<char
,string_char_traits<char>,__default_alloc_template<false,0> >,__default_alloc_te
mplate<false,0> >::at (int)'
test.cpp:187: no matching function for call to `vector<basic_string<char
,string_char_traits<char>,__default_alloc_template<false,0> >,__default_alloc_te
mplate<false,0> >::at (int)'
test.cpp:188: no matching function for call to `vector<basic_string<char
,string_char_traits<char>,__default_alloc_template<false,0> >,__default_alloc_te
mplate<false,0> >::at (int)'
test.cpp:189: no matching function for call to `vector<basic_string<char
,string_char_traits<char>,__default_alloc_template<false,0> >,__default_alloc_te
mplate<false,0> >::at (int)'
test.cpp:190: no matching function for call to `vector<basic_string<char
,string_char_traits<char>,__default_alloc_template<false,0> >,__default_alloc_te
mplate<false,0> >::at (int)'
test.cpp:191: no matching function for call to `vector<basic_string<char
,string_char_traits<char>,__default_alloc_template<false,0> >,__default_alloc_te
mplate<false,0> >::at (int)'
Anyone knows what might be wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
std::vector::at()
接受一个size_type
参数,该参数是无符号的。也许这有效:std::vector::at()
takes asize_type
argument, which is unsigned. Perhaps this works:应该是:
你能告诉我你读的哪本书告诉你用 good() 函数测试流吗?我真的很想知道这一点。
编辑:在我看来,好像您从未填充零件向量。 g_string 是什么样的东西? (除了,咳咳,成人的意思)。
should be:
Can you please tell me which book you read told you to test the stream with the good() function? I genuinely want to know this.
Edit: It looks to me as if you never populate the parts vector. And what sort of thing is a g_string? (apart from the, ahem, adult meaning).