[英]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.