简体   繁体   English

错误:&#39;std::__cxx11::list<User> ::iterator&#39; {aka &#39;struct std::_List_iterator<User> &#39;} 没有名为 XXX 的成员

[英]error: 'std::__cxx11::list<User>::iterator' {aka 'struct std::_List_iterator<User>'} has no member named XXX

I am learning C++ but I get an error with my first class.我正在学习 C++,但是我的第一堂课出错了。 I need help to understand what it is wrong with this code.我需要帮助来理解这段代码有什么问题。

I get the following error message:我收到以下错误消息:

main.cpp:36:13: error: 'std::__cxx11::list<User>::iterator' {aka 'struct std::_List_iterator<User>'} has no member named
 'printInfo'
         usr.printInfo();
             ^~~~~~~~~

The main function prompts users to enter their name, creates list and list iterator, displays Info of the two users main函数提示用户输入姓名,创建列表和列表迭代器,显示两个用户的Info

The main() function: main()函数:

#include <iostream>
#include <list>
#include "User.cpp"
using namespace std;
int main() {
    string str0 ="";
    usrs players; 
    players = create_2user();
    list<User> playerList = { players.usr0, players.usr1 };
    for( list<User>::iterator usr = playerList.begin(); usr != playerList.end(); usr++) {
        usr.printInfo();
    }       
   return 0;
}

The class User :User

class User {
    string name;
    public:
        User();      
        void set_name (string in_name);     
        string get_name();
        void printInfo();
};
User::User()  { name="Unkonw"; }
void User::set_name(string in_name) { name  = in_name; }
string User::get_name() { return name; }
void   User::printInfo() { cout<<name; }

struct usrs { User usr0, usr1; };

usrs create_2user() {
    User array_usr[2];
    string str0;

    for(int i=0;i<2;i++) {
        cout<<"Enter player "<<i<<"'s name: ";
        cin>>str0;
        array_usr[i].set_name(str0);
    }
    usrs result = { array_usr[0], array_usr[1] };
    return result;
}

You try to invoke the member function printInfo on an iterator to a player instance, not a user instance.您尝试在迭代器上调用成员函数printInfo到播放器实例,而不是用户实例。 You can fix this by您可以通过以下方式解决此问题

for( list<User>::iterator usr = playerList.begin(); usr != playerList.end(); usr++) {
    usr->printInfo();
}

or with a range base for loop, which does the dereferencing for you:或使用范围基础 for 循环,它为您取消引用:

for (User& usr : playerList)
    usr.printInfo(); // Here, your original syntax works

If you want to overengineer it, you could also begin learning the <algorithm> header and如果您想对其进行过度设计,您还可以开始学习<algorithm>标头和

#include <algorithm>

std::for_each(playerList.cbegin(), playerList.cend(),
    std::mem_fn(&User::printInfo));

which in C++20 won't be as absurd for a beginner as the above usage:对于初学者来说,在 C++20 中不会像上述用法那样荒谬:

std::ranges::for_each(playerList, &User::printInfo);

Note that as printInfo() doesn't alter any state of the object, you should mark this member function const , and use a const_iterator , within the range base for loop, for (const User& usr : playerList) and for the algorithm invocation cbegin()/cend() .请注意,由于printInfo()不会改变对象的任何状态,因此您应该将此成员函数标记为const ,并在 for 循环、 for (const User& usr : playerList)和算法调用cbegin()/cend()范围内使用const_iterator cbegin()/cend()

This: list<User>::iterator usr declares usr to be an iterator.这个: list<User>::iterator usr声明usr是一个迭代器。 In C++, an iterator is something like a generalised pointer — an object which points to another object (1) .在 C++ 中,迭代器类似于广义指针——指向另一个对象(1) 的对象。 So you have to treat it like a pointer: if you want to access the object being pointed to, you have to derefernce the iterator using either * (to get to the object), or -> (to get to the object's members).所以你必须把它当作一个指针来对待:如果你想访问被指向的对象,你必须使用* (访问对象)或-> (访问对象的成员)来取消引用迭代器。 So change the statement in the for loop body to:所以将for循环体中的语句改为:

usr->printInfo();

(1) The difference is that pointers point to an address, while iterators point to a position in a container. (1)区别在于指针指向地址,而迭代器指向容器中的位置。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误:'operator&lt;&lt;' 不匹配(操作数类型是 'std::ostream' {aka 'std::basic_ostream<char> '} 和 'std::_List_iterator<int> ')</int></char> - error: no match for ‘operator<<’ (operand types are ‘std::ostream’ {aka ‘std::basic_ostream<char>’} and ‘std::_List_iterator<int>’) 错误:无法转换 'std::__cxx11::basic_string<char> ::迭代器'</char> - error: cannot convert ‘std::__cxx11::basic_string<char>::iterator’ cpp:错误:'类提升::可选<std::__cxx11::basic_string<char> &gt;' 没有名为 'c_str' 的成员</std::__cxx11::basic_string<char> - cpp: error: 'class boost::optional<std::__cxx11::basic_string<char> >' has no member named 'c_str' &#39;multiline&#39;不是&#39;std :: __ cxx11 :: regex&#39;的成员 - ‘multiline’ is not a member of ‘std::__cxx11::regex’ 错误:为&#39;operator std::string {aka std::__cxx11::basic_string 指定的返回类型<char> }&#39; - Error:return type specified for 'operator std::string {aka std::__cxx11::basic_string<char>}' 无法转换 'std::__cxx11::string {aka std::__cxx11::basic_string<char> }' 到 'LPCSTR {aka const char*}'</char> - cannot convert 'std::__cxx11::string {aka std::__cxx11::basic_string<char>}' to 'LPCSTR {aka const char*}' 错误无法转换 &#39;coll&#39;(类型 &#39;std::__cxx11::list<char> &#39;) 输入 &#39;const char&amp;&#39; - error cannot convert 'coll' (type 'std::__cxx11::list<char>') to type 'const char&' 错误:将 'const std::__cxx11::list&lt;&gt;' 作为 'this' 参数传递会丢弃限定符 - error: passing ‘const std::__cxx11::list<>’ as ‘this’ argument discards qualifiers 错误:'operator+' 不匹配(操作数类型为 'std::__cxx11::list<int> ' 和 'int')|</int> - Error: no match for 'operator+' (operand types are 'std::__cxx11::list<int>' and 'int')| 字符串未正确复制并显示错误:无法转换 'std::__cxx11::string {aka std::__cxx11::basic_string<char> }'</char> - string not copying correctly and showing error: cannot convert 'std::__cxx11::string {aka std::__cxx11::basic_string<char>}'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM