I am learning C++ but I get an error with my first class. 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
The main()
function:
#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
:
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. 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 (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
#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:
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()
.
This: list<User>::iterator usr
declares usr
to be an iterator. In C++, an iterator is something like a generalised pointer — an object which points to another object (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:
usr->printInfo();
(1) The difference is that pointers point to an address, while iterators point to a position in a container.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.