没有匹配的函数可供调用

发布于 2024-11-06 08:31:59 字数 2353 浏览 0 评论 0原文

我得到了这种方法,我从文件中获取用户数据行,然后将这些行分成几个部分,然后将这些部分放入不同的字符串中。最后,我创建一个新的用户对象并将该用户对象添加到另一个名为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

望笑 2024-11-13 08:31:59

std::vector::at() 接受一个 size_type 参数,该参数是无符号的。也许这有效:

         string usernr = parts.at(0u);
         string name = parts.at(1u);
         string gender = parts.at(2u);
         string age = parts.at(3u);
         string profession = parts.at(4u);
         string email = parts.at(5u);

std::vector::at() takes a size_type argument, which is unsigned. Perhaps this works:

         string usernr = parts.at(0u);
         string name = parts.at(1u);
         string gender = parts.at(2u);
         string age = parts.at(3u);
         string profession = parts.at(4u);
         string email = parts.at(5u);
橙幽之幻 2024-11-13 08:31:59
 while ( file.good() )
    {
       getline (file,line);

应该是:

 while ( getline (file,line) ) 

你能告诉我你读的哪本书告诉你用 good() 函数测试流吗?我真的很想知道这一点。

编辑:在我看来,好像您从未填充零件向量。 g_string 是什么样的东西? (除了,咳咳,成人的意思)。

 while ( file.good() )
    {
       getline (file,line);

should be:

 while ( getline (file,line) ) 

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文