[英]No suitable conversion exists from to * exists
I am using linked lists to manipulate a sorted list.我正在使用链表来操作排序列表。 However, in my function PutUser, I am getting an error when calling on another function to compare two users.
但是,在我的函数 PutUser 中,调用另一个函数来比较两个用户时出现错误。 I am not sure what to do or how to handle the error.
我不确定该怎么做或如何处理错误。
Here is my NodeUser struct (creates nodes for the linked list):这是我的 NodeUser 结构(为链表创建节点):
struct NodeUser {
User user;
NodeUser* nextOnList;
};
I then have a function that is suppose to create a new node and put a user into the list然后我有一个函数,假设创建一个新节点并将用户放入列表
void SortedList::PutUser(User* user) {
NodeUser* newNode; /
NodeUser* previousPointer;
NodeUser* location;
bool moreToSearch;
location = topOfList;
previousPointer = NULL;
moreToSearch = (location != NULL);
while (moreToSearch) {
switch (user->comparedTo(location->user)) {
case GREATER:
previousPointer = location;
location = location->next;
moreToSearch = (location != NULL);
break;
case LESS:
moreToSearch = false;
break;
}
}
newNode = new NodeUser;
newNode->user = user;
if (previousPointer == NULL) {
newNode->next = topOfList;
topOfList = newNode;
}
else {
newNode->next = location;
previousPointer->next = newNode;
}
length++;
}
I am getting the error at (specifically location is underlined in red (error)):我收到错误(特别是位置用红色下划线(错误)):
switch (user->comparedTo(location->user))
How to fix the error at the switch?如何修复交换机上的错误?
You are trying to pass a User
object where a User*
pointer is expected.您正试图在需要
User*
指针的地方传递User
对象。
There is no reason to pass a User
object by pointer into SortedUserType::PutUser()
or User::comparedTo()
.没有理由通过指针将
User
对象传递给SortedUserType::PutUser()
或User::comparedTo()
。 Pass it in by reference instead:而是通过引用传递它:
class User {
public:
...
RelationType comparedTo(const User &aUser) const;
...
};
void SortedUserType::PutUser(const User &user) {
...
while (moreToSearch) {
switch (user.comparedTo(location->user)) {
...
}
...
newNode = new NodeUser;
newNode->user = user;
...
}
A quick fix would be to一个快速的解决方法是
RelationType comparedTo(User const &aUser) const;
Or at least或者至少
switch (user->comparedTo(&location->user))
Pointers should not be overused.不应过度使用指针。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.