Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/139.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 警告:控件可能达到非无效功能的末尾?下课时_C++_Class_Return - Fatal编程技术网

C++ 警告:控件可能达到非无效功能的末尾?下课时

C++ 警告:控件可能达到非无效功能的末尾?下课时,c++,class,return,C++,Class,Return,我有这个函数,它将在向量中搜索与借用ID匹配的ID,然后返回具有匹配ID的借用者。 但是我一直收到这个警告 Borrower getborrowerbyID(string ID) { for(int i =0; i<Borrowlist.size();i++) { if(Borrowlist[i].getID()==ID) { return Borrowlist[i]; } } } bo

我有这个函数,它将在向量中搜索与借用ID匹配的ID,然后返回具有匹配ID的借用者。 但是我一直收到这个警告

Borrower getborrowerbyID(string ID)  
{
    for(int i =0; i<Borrowlist.size();i++)
    {
        if(Borrowlist[i].getID()==ID)
        {
            return Borrowlist[i];

        }
    }
}
bookergetbornerbyid(字符串ID)
{

对于(int i=0;i我将返回success status,并通过如下函数参数传递匹配:

bool getborrowerbyID(string ID, Borrower& refBorrower)  
{
    for(int i =0; i<Borrowlist.size();i++)
    {
        if(Borrowlist[i].getID()==ID)
        {
            refBorrower = Borrowlist[i];
            return true; // signall sucess match found
        }
    }
    return false; // signal falure, ie. no match
}

我将返回sucess status,并通过如下函数参数传递匹配:

bool getborrowerbyID(string ID, Borrower& refBorrower)  
{
    for(int i =0; i<Borrowlist.size();i++)
    {
        if(Borrowlist[i].getID()==ID)
        {
            refBorrower = Borrowlist[i];
            return true; // signall sucess match found
        }
    }
    return false; // signal falure, ie. no match
}

当找不到搜索ID时,您确实不会返回。您可能会更改为:

Borrower& getBorrowerByID(const std::string& ID)  
{
    auto it = std::find_if(Borrowlist.begin(), Borrowlist.end(),
                           [&](const Borrower& borrower){ return borrower.getID() == ID; });

    if (it == Borrowlist.end()) {
        throw std::runtime_error("Not found");
    }
    return *it;
}
或返回指针:

Borrower* getBorrowerByID(const std::string& ID)  
{
    auto it = std::find_if(Borrowlist.begin(), Borrowlist.end(),
                           [&](const Borrower& borrower){ return borrower.getID() == ID; });

    if (it == Borrowlist.end()) {
        return nullptr; // Not found
    }
    return std::addressof(*it); // or &*it assuming no evil overload of unary operator& 
}

当找不到搜索ID时,您确实不会返回。您可能会更改为:

Borrower& getBorrowerByID(const std::string& ID)  
{
    auto it = std::find_if(Borrowlist.begin(), Borrowlist.end(),
                           [&](const Borrower& borrower){ return borrower.getID() == ID; });

    if (it == Borrowlist.end()) {
        throw std::runtime_error("Not found");
    }
    return *it;
}
或返回指针:

Borrower* getBorrowerByID(const std::string& ID)  
{
    auto it = std::find_if(Borrowlist.begin(), Borrowlist.end(),
                           [&](const Borrower& borrower){ return borrower.getID() == ID; });

    if (it == Borrowlist.end()) {
        return nullptr; // Not found
    }
    return std::addressof(*it); // or &*it assuming no evil overload of unary operator& 
}

如果
借用列表
中没有匹配项,会发生什么情况?
返回
将永远不会被调用,并且函数将在最后退出循环。您需要找到某种方式向调用代码发出信号,表明未找到匹配项,然后返回该匹配项。顺便说一句,如果您只是在
向量
中搜索一个匹配,STL中有一些方法可以帮你实现。正如Steve指出的,如果找不到ID会发生什么?你在函数末尾没有返回语句。那么函数应该返回什么?另外,检查一下,使用一些
throw
at last语句。如果
中没有匹配怎么办st
?将永远不会调用
返回值,函数将在最后退出循环。您需要找到某种方式向调用代码发出信号,表明尚未找到匹配项,然后返回该值。顺便说一句,如果您只是在
向量中搜索匹配项,则STL中有一些方法可以执行以下操作:正如Steve所指出的,如果找不到ID会发生什么?你在函数末尾结束时没有返回语句。那么函数应该返回什么?另外,请检查一下使用一些
throw
at last语句。