C++ 为什么我会得到这样的警告;绑定对基本类型“U字符串”的引用;?
我从一个函数中调用一个函数,并通过这两个函数传递一个参数作为常量引用。当我编译的时候,我得到警告(还有智能感知),它是 对基本类型字符串的引用绑定 发生什么事了 我以为我了解情况,但我想我不太清楚。这些答案(,)都涉及到这个问题的形式。 我想,因为调用函数的参数是一个C++ 为什么我会得到这样的警告;绑定对基本类型“U字符串”的引用;?,c++,reference,constants,C++,Reference,Constants,我从一个函数中调用一个函数,并通过这两个函数传递一个参数作为常量引用。当我编译的时候,我得到警告(还有智能感知),它是 对基本类型字符串的引用绑定 发生什么事了 我以为我了解情况,但我想我不太清楚。这些答案(,)都涉及到这个问题的形式。 我想,因为调用函数的参数是一个常量&,并且它是作为常量&传递的,所以我应该很好。下面是我的代码片段 我的函数定义: std::string joinPathAndFile(const std::string &path, const std::strin
常量&
,并且它是作为常量&
传递的,所以我应该很好。下面是我的代码片段
我的函数定义:
std::string joinPathAndFile(const std::string &path, const std::string &fname)
{
boost::filesystem::path b_fname (fname);
boost::filesystem::path b_path (path);
boost::filesystem::path b_full_path = b_path / b_fname;
return b_full_path.string();
}
void myFunction(const std::string &path)
{
std::string file = joinPathAndFile(path, "fname.txt");
std::cout << file << std::endl;
}
int main()
{
myFunction("/path/to/");
}
std::string joinPathAndFile(const std::string&path,const std::string&fname)
{
boost::filesystem::path b_fname(fname);
boost::filesystem::path b_path(path);
boost::filesystem::path b_full_path=b_path/b_fname;
返回b_full_path.string();
}
void myFunction(const std::string和path)
{
std::string file=joinPathAndFile(路径,“fname.txt”);
std::难道我看不出你显示的代码有任何问题吗。@5gon12eder:我也这么认为!你在使用什么编译器?这是完整的警告消息吗?也许你把它简化了。你能发布一个实际上可以编译并且仍然触发警告的最小程序吗(请参阅“”)?您是否尝试单独编译发布的代码,但仍然会产生警告?如果是,如果将joinPathAndFile
的实现更改为return path+“/,问题是否仍然存在+fname;
并摆脱Boost?像这样的有线问题通常都有有线原因,因此我们能够准确地看到触发错误的代码,并且代码尽可能少,这一点很重要。您是对的,在这个网站上有许多问题不是很好的例子。;-)