How to return NULL string

Q:

std::string get_file_contents(const char *filename)
{
std::ifstream in(filename, std::ios::in | std::ios::binary);
if (in)
{
std::string contents;
in.seekg(0, std::ios::end);
contents.resize(in.tellg());
in.seekg(0, std::ios::beg);
in.read(&contents[0], contents.size());
in.close();
return(contents);
}

}
View Code

waning:

lane_seg.cpp: In function ‘std::__cxx11::string get_file_contents(const char*)’:
lane_seg.cpp:303:1: warning: control reaches end of non-void function [-Wreturn-type]

A:

warning意思是:控制到达非void函数的结尾。就是说你的一些本应带有返回值的函数到达结尾后可能并没有返回任何值。这时候,最好检查一下是否每个控制流都会有返回值。

std::string get_file_contents(const char *filename)
{
    std::ifstream in(filename, std::ios::in | std::ios::binary);
    if (in)
    {
        std::string contents;
        in.seekg(0, std::ios::end);
        contents.resize(in.tellg());
        in.seekg(0, std::ios::beg);
        in.read(&contents[0], contents.size());
        in.close();
        return(contents);
    }
    return NULL;//or return " "; 
    
}

End

猜你喜欢

转载自www.cnblogs.com/happyamyhope/p/9070693.html