const char to std string conversion

ol muser
ol muser used Ask the Experts™
on
What is the simplest way to the conversion below?
kconn.cpp:35:15: error: non-const lvalue reference to type 'std::string' (aka 'basic_string<char, char_traits<char>, allocator<char> >') cannot bind to a value of unrelated type 'const char [4]'
            addRoute("JFK", "ATL", 150);
                     ^~~~~
kconn.cpp:27:31: note: passing argument to parameter 's' here
        void addRoute(std::string &  s,

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Senior Software Engineer (Avast)
Commented:
std::string & needs to be either

std:: string // by value
std:: string && // by r-value reference
std:: string const & // by const l-value reference

It cannot be a non-const l-value reference. This is because the conversion creates a temporary, which cannot be bound to a non-const l-value reference.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial