C++ std::list<T>::iterator Does the iterator have acces to <T> member functions?

StackOverflow https://stackoverflow.com/questions/20131994

  •  03-08-2022
  •  | 
  •  

Вопрос

This is my first time posting here at SO, but I've been utilizing the answers and help here for a while now.

I'm working on a simple card game right now that I've been planning on and off for a few years. Ultimately it's going to be my senior project for school(not asking anyone to do my homework for me!)

I'm experienceing an issue right now when trying to pint the contents of a list to the console through a couple classes.

for(std::list<Card>::iterator it = Hand.begin(); it != Hand.end(); it++)
    std::cout<< *it.GetName();

I'm trying to figure out if the line

std::cout<< *it.GetName();

is going to be valid or not. I'm troubleshooting from work and I don't have the ability to try anything right now. Before I had to drop the project this morning I was having some compile errors that lead me to believe I needed to overload the << operator which I tried and ended up getting a couple linking errors for some reason.

I've been researching most of the day, but haven't come across any situations that jumped out at me as the correct solution, but this little block is what I've found that seems the most correct for what I'm trying to do.

Will that block print the names of the Card Objects stored in the Hand list as is right now? Yes, the Card Class does have that GetName() function and it is declared correctly.

Это было полезно?

Решение

*it.getName();

is the same as

*(it.getName());

since . and function call have higher precedence than unary *. So the compiler looks for a member getName in class std::list<Card>::iterator, but there isn't one: getName is (I assume) a member of Card, and iterators don't have any inheritance or forwarding relationship to their object.

You probably meant

(*it).getName();

or equivalently,

it->getName();

(The arrow -> operator is just a short cut for dereferencing and then naming a member.)

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top