samedi 19 septembre 2020

Use of undeclared identifier after if/else if branch

P* pFactory(const std::string& pName){
    if (p == "s")
        SP newP;
    else if(pName == "f")
        FP newP;
    else if(pName == "l")
        LP newP;
    else if(pName == "i")
        IP newP;
    else if(pName == "g")
        GP newP;
    else if (pName == "n")
        NP newP;

    P* p_ptr = &newP;

return nullptr;
}

Basically, the function is supposed to return a pointer to a new P object that gets made depending on the pName input. I'm getting a Use of undeclared identifier 'newP' error on the pointer line.

I thought it could be because there's no final else so the object might never be created, but replacing the last else if with else didn't help. What's causing this error?

Aucun commentaire:

Enregistrer un commentaire