2

I am calling a function from within a function and passing a parameter as a const reference through both. When I compile I get warned (and also by the intellisense) that it is

binding of reference to type basic_string

What's going on?

I thought I understood the situation, but I guess I'm not clear on it. These answers (1, 2, 3) all touch on forms of this problem. I thought because the parameter of the calling function is a const & and it's being passed as a const &, I should be good. a snippet of my code is below.

My function definition:

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/");
}
Community
  • 1
  • 1
marcman
  • 3,233
  • 4
  • 36
  • 71
  • I cannot see any issue with the code you've shown. – 5gon12eder Sep 17 '16 at 01:44
  • @5gon12eder: I thought so too! – marcman Sep 17 '16 at 01:46
  • 1
    What compiler are you using? And is this the full warning message? – AnT stands with Russia Sep 17 '16 at 01:49
  • Maybe you've oversimplified it. Could you post a minimal program that can actually be compiled and still triggers the warning (see “[mcve]”)? – 5gon12eder Sep 17 '16 at 01:49
  • This compiles fine for me, but then I have no code in the joinPathAndFile() function. Perhaps you could post that code, or see if the problem is caused by something in that function – hvanbrug Sep 17 '16 at 01:49
  • @5gon12eder: It really is this simple I swear. But I did update my OP – marcman Sep 17 '16 at 01:55
  • What line is the warning on? Seems like that would narrow it down quite a bit. – Retired Ninja Sep 17 '16 at 02:11
  • After adding `#include `, `#include ` and `#include `, both GCC and Clang compile the code happily even with `-Wall -Wextra -pedantic`. – 5gon12eder Sep 17 '16 at 02:14
  • Interesting. Maybe I have some weird flag set. I dunno. Also sorry for ignoring the includes (I noticed they;re often left out on SO) – marcman Sep 17 '16 at 02:15
  • 2
    Did you try compiling the posted code in isolation and did it still produce the warning? If so, does the problem persist if you change the implementation of `joinPathAndFile` to `return path + "/" + fname;` and get rid of Boost? Wired problems like this usually have wired causes so it is important that we can see exactly the code that triggers the error and that code is as minimal as possible. And you are right that there are many questions on this site that are not a good example for this. ;-) – 5gon12eder Sep 17 '16 at 02:20
  • It probably would be helpful if you could tell us where exactly the warning is occurring and what the entire warning message is. – Dan Sep 24 '16 at 07:54

0 Answers0