0

I have two files: error.h and error.cpp. Compiling with

g++ -std=c++0x 

gives me an error:

error.cpp:9:33:**call of overloaded "to_string(char*&)" is ambiguous** 

How can i fix this problem?

error.h:

 1 #ifndef ERROR_H_GUARD 2 #define ERROR_H_GUARD 4 #include <string> 6 class Error { 7 public: 8 Error(int pos, std::string& msg); 10 Error(int pos, char* msg); 12 const char* what() throw(); 14 private: 15 std::string msg; 17 void setMsg(int pos, std::string& msg); 18 }; 19 20 #endif 

error.cpp:

 2 #include "error.h" 4 Error::Error(int pos, std::string& msg){ 5 setMsg(pos, msg); 6 } 8 Error::Error(int pos, char* msg) { 9 setMsg(pos, std::to_string(msg)); 10 } 12 const char* Error::what() throw() { 13 return msg.c_str(); 14 } 16 void Error::setMsg(int pos, std::string& msg){ 17 this->msg = std::to_string(pos) + msg + std::string("\n") + std::string(pos - 1, ' ') + std::string("^"); 18 } 
2
  • 4
    char* msg should be const char* msg. And your std::string& should be const std::string&. Remeber to use const by default. Commented Jan 29, 2013 at 21:30
  • You could have made easily a sscce and post that (if you then wouldn't have found it out yourself). Commented Jan 29, 2013 at 21:34

5 Answers 5

3

std::to_string takes integer as parameter, but you pass a pointer to it.

Error::Error(int pos, char* msg) { setMsg(pos, std::to_string(msg)); } 

You don't need to translate a string to string, try:

Error::Error(int pos, char* msg) { setMsg(pos, std::string(msg)); } 

Side Note: all your function parameter better take const reference:

Error(int pos, const std::string& msg); void setMsg(int pos, const std::string& msg); 
Sign up to request clarification or add additional context in comments.

Comments

2

to_string() is used to convert something which is not a string (e.g. a long, an int, etc.) into a string. You have a char*, which is a C string, and what you want to do is to create a string object out of it, not convert it.

Your compiler complains about ambiguity because it cannot find a version of to_string() for the type you are passing to it (char*), which makes sense, considering the purpose of that function.

If you declared your parameter string const& rather than string& in the corresponding overload of setMsg() (and in the constructor of Error as well), you could directly invoke it by passing C strings: a temporary of type string would be created automatically and bound to the argument of setMsg().

This way you would even get rid of the specific overload of setMsg() for C strings, which in fact does nothing but forwarding.

Comments

2

Use string's constructor instead:

std::string(msg) 

However note that this temporary can't bind to the reference argument. You'll have to fix that.

Maybe like this:

Error::Error(int pos, char* msg) { std::string str(msg); setMsg(pos, msg); } 

Or use const-ref.

2 Comments

That won't work here, because the Error constructor requires a non-const reference. That seems like a bug in the design, though.
No need to use the constructor explicitly, passing the const char* would also work.
1

Drop Error(int pos, char* msg) and change the remaining constructor and setMsg() to

Error(int pos, const std::string& msg); ... void setMsg(int pos, const std::string& msg); 

When you call Error() with a char*, it will automatically use the std::string constructor. So, there's no need for a separate constructor.

Comments

1

This won't work:

Error::Error(int pos, char* msg) { setMsg(pos, std::to_string(msg)); } 

Because std::to_string() takes a numeric value to convert. You probably meant:

Error::Error(int pos, char const * msg) { setMsg(pos, msg); } 

Which is exactly the same as the std::string& version (which, in turn, should be std::string const &), so you could actually just remove this char* constructor (less code to maintain: bonus)!

Also this:

void Error::setMsg(int pos, std::string& msg){ 

Should probably be this:

void Error::setMsg(int pos, std::string const & msg){ 

1 Comment

won't work either because of the non-const string reference of setMsg.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.