[英]Why am I getting expression must have pointer to class type in c++?
Hey I am trying compare values of vector that store class objects to another vector of same class.嘿,我正在尝试将存储 class 对象的向量值与相同 class 的另一个向量进行比较。 I have class in seperate.h file containing following
我在包含以下内容的单独.h 文件中有 class
class Student{
std::string ID;
std::string Name;
std::vector<std::string> FriendsIDs
Student(std::string ID, std::string Name, std::vector<std::string> FriendsIDs)
~Student(){}
};
and the class goes so on... the values I stored in objects are like和 class 等等......我存储在对象中的值就像
std::vector<Student* > List_Students = { {"123", "Sam", {"234", "435"}}, {"234", "Shane", {"435"}}, {"435", "Mitch", {"123"}}};
and so on and on the list goes (I read it from file, those are just few for testing), I want to compare the FriendsIDs with ID and if it matches it should print friends name with thier IDs.依此类推,在列表中(我从文件中读取它,这些只是用于测试的少数),我想将 FriendsID 与 ID 进行比较,如果匹配,则应使用他们的 ID 打印朋友姓名。 Expected output:
预期 output:
123, sam, 234(shane), 435(mitch)
234, shane, 435(mitch)
etc I used the bottom code that i inherit from stackoverflow, which was working until i put class into seperate.h file.等我使用了从stackoverflow继承的底部代码,该代码一直有效,直到我将class放入seperate.h文件中。 I get error at last line of code (iter->Name), the error read "expression must have pointer to class type".
我在代码的最后一行(iter->Name)得到错误,错误读取“表达式必须有指向 class 类型的指针”。 My guess is that iter doesn't know what Name is, if so how do I point or fix this issue please.
我的猜测是 iter 不知道 Name 是什么,如果是,我该如何指出或解决这个问题。 thank you
谢谢你
for (Student* s : List_Students)
{
for (string& f : s->FriendsID)
{
auto iter = find_if(List_Students.begin(), List_Students.end(), [&](Student* sf) { return sf->ID == f; });
if (iter != List_Students.end())
{
cout << " ( " << iter->Name << " ) " << "\n";
}
}
}
List_Students
is a vector of Student*
, so iter
"points at" Student*
. List_Students
是Student*
的向量,因此iter
“指向” Student*
。 Therefore you have to do "dereferencing" two times to access corresponding Student
.因此,您必须进行两次“取消引用”才能访问相应的
Student
。
Therefore, you should use (*iter)->Name
(or (**iter).Name
) instead of iter->Name
.因此,您应该使用
(*iter)->Name
(或(**iter).Name
)而不是iter->Name
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.