简体   繁体   中英

invalid conversion from 'void*' to class(c++)

Ok, so here's my code:

StatusType System::AddJobSearcher(void *DS, int engineerID, int reqSalary) {
    if(DS == NULL || engineerID < 0 || reqSalary < 0) return INVALID_INPUT;
    System* system = DS;
    Engineer* engineer = new Engineer(engineerID, reqSalary);
    if(!engineer) return ALLOCATION_ERROR;
    if(system->All.isIn(*engineer->generateIdKey())) { 
        delete(engineer);
        return FAILURE;
    }
    system->All.insert(*engineer->generateIdKey(), *engineer); 
    return SUCCESS;
}

Now, system is a class and DS is supposed to be pointer to one. When I try to point newly created system to DS(System* system = DS;) I get:

invalid conversion from 'void*' to 'System*' [-fpermissive]

How can I solve that

Since you know DS will be of type System* , you should change the argument type:

StatusType System::AddJobSearcher(System* DS, int engineerID, int reqSalary) {
//                                ^^^^^^

And if you happen to pass a void* as first argument, you should refactor your code so that you don't have to.

In C++ (opposite to C) you may not implicitly convert a pointer of type void * to a pointer of other type. You have to do this explicitly

System* system = static_cast<System*>( DS );

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM