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 }
char* msgshould beconst char* msg. And yourstd::string&should beconst std::string&. Remeber to use const by default.