error: void value not ignored as it ought to be

后端 未结 3 2115
孤城傲影
孤城傲影 2021-02-18 20:02
template  Z myTemplate  :: popFromVector ()
{
    if (myVector.empty () == false)
        return myVector.pop_back ();

    return 0;
}

int m         


        
3条回答
  •  既然无缘
    2021-02-18 20:42

    That is because the definition of std::vector::pop_back has a void return type ... you are trying to return something from that method, which won't work since that method doesn't return anything.

    Change your function to the following so you can return what's there, and remove the back of the vector as well:

    template  Z myTemplate  :: popFromVector ()
    {
        //create a default Z-type object ... this should be a value you can easily
        //recognize as a default null-type, such as 0, etc. depending on the type
        Z temp = Z(); 
    
        if (myVector.empty () == false)
        {
            temp = myVector.back();
            myVector.pop_back();
            return temp;
        }
    
        //don't return 0 since you can end-up with a template that 
        //has a non-integral type that won't work for the template return type
        return temp; 
    }
    

提交回复
热议问题