简体   繁体   English

不存在从到 * 的合适转换存在

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

相关问题 不存在从“ std :: string”到“ char”的适当转换 - no suitable conversion from “std::string” to “char” exists C++ 没有合适的转换 function from to 存在 - C++ no suitable conversion function from to exists OGRE不存在合适的转换函数 - OGRE No suitable conversion function exists 从“ std :: string”到“ PVOID”的转换功能不存在 - no suitable conversion function from “std::string” to “PVOID” exists C ++不存在从“日期”到“日期(*)()”的合适转换函数 - C++ no suitable conversion function from “Date” to “Date (*)()” exists 如何修复“没有合适的转换函数从”String“到”const char *“存在”? - How to fix “no suitable conversion function from ”String“ to ”const char *“ exists”? C++ 不存在从 INPUT 到 LPINPUT 的合适转换 function - C++ no suitable conversion function from INPUT to LPINPUT exists 不存在从LoadTask :: MasterFilePtr到MasterFile的合适转换函数* - No Suitable Conversion Function from LoadTask::MasterFilePtr to MasterFile * exists 不存在从“std::string”到“const char *”的合适转换函数 - No suitable conversion function from “std::string” to “const char *” exists 不存在从“cv::Mat”到“IplImage”的合适的用户定义转换 - No suitable user-defined conversion from "cv::Mat" to "IplImage" exists
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM