![]() |
|
|
Welcome to the { mindfrost82.com } forums. You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today! If you have any problems with the registration process or your account login, please contact contact us. |
|
|||||||
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
|
|||
|
error C2664
Hi,
I have to a project which I am converting it to Unicode. I am getting following error. I am not sure How to fix it. error C2664: 'LogFile::LogToFile' : cannot convert parameter 1 from 'const unsigned short [65]' to 'std::string' The code is #define MAIN_LOG_FILE ".\\ABC.log" Defination LogToFile(std::string message, std::string fileName); I am calling this LogToFile("My Name", MAIN_LOG_FILE) I have tried using "L" and "_T" It is not working. Can you please help? Thanks -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |
|
|||
|
Re: error C2664
On Aug 1, 7:28 pm, Trups <Samant.Tru...@gmail.com> wrote:
> error C2664: 'LogFile::LogToFile' : cannot convert parameter 1 from > 'const unsigned short [65]' to 'std::string' > > The code is > #define MAIN_LOG_FILE ".\\ABC.log" > > Defination LogToFile(std::string message, std::string fileName); > > I am calling this LogToFile("My Name", MAIN_LOG_FILE) > > I have tried using "L" and "_T" It is not working. > > Can you please help? > Thanks Is that the real code you are using? "L" not "_T" would help here why he is trying to reference the 1st parameter as an unsigned short[65]? something tells me that you haven't gave us the "real" code please re- post -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |
|
|||
|
Re: error C2664
Trups wrote:
> Hi, > > I have to a project which I am converting it to Unicode. I am > getting following error. I am not sure How to fix it. > > error C2664: 'LogFile::LogToFile' : cannot convert parameter 1 from > 'const unsigned short [65]' to 'std::string' > > The code is > #define MAIN_LOG_FILE ".\\ABC.log" > > Defination LogToFile(std::string message, std::string fileName); > > I am calling this LogToFile("My Name", MAIN_LOG_FILE) > > I have tried using "L" and "_T" It is not working. Just looking at the code everything looks fine for me "My Name" and MAIN_LOG_FILE is const char * which the compiler casts into std::string behind your back and would call LogToFile(...) with that. I assume you use a magic compiler switch which tells it to treat all string literals as const wchar_t *. Well in that case it would have to be. LogToFile(const std::wstring &, const std::wstring & fileName); (The const & is optional, but I think that's what you intended). But then you're running into trouble with the fileName argument. I'm not aware of any fstream.open or (f|)open method/function which takes a const wchar_t * argument, but may be that's just because of my limited knowledge. You could make it a templated function like... <snip> // Transforms a std::basic_string<CharT> to std::basic_string<char> // A lot of std::locale and facet<codecvt<...> > magic would be required // in the real world. template<class CharT> std::basic_string<char> str_conv(const std::basic_string<CharT> & s) { return std::basic_string<char>("dummy_for_code_conversion "); } // Specialization for CharT==char // template<> std::basic_string<char> str_conv<char>(const std::basic_string<char> & s) { return s; } // This one opens a file. Logs the string // and closes it. May be it would be better // to pass around a ref to basic_ofstream<..> // instead of the file name. And in that case // You could simply use the // std::basic_ostream<CharT> & // operator<<(std::basic_ostream & os, // const std::basic_string<CharT> &s); // operator anyway. template<class CharT> bool LogToFile(const std::basic_string<CharT> & s, const std::basic_string<CharT> & fn) { std::basic_ofstream<CharT> fs(str_conv(fn).c_str()); fs.imbue(std::locale("en_US.utf-8")); fs << s << std::endl; return true; }; // an overload that transforms const CharT * to // std::basic_string<CharT>. template<class CharT> bool LogToFile(const CharT * s,const CharT * fn) { return LogToFile(std::basic_string<CharT>(s), std::basic_string<CharT>(fn)); } </snip> HTH O. -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |
|
|||
|
Re: error C2664
On Aug 1, 7:28 pm, Trups <Samant.Tru...@gmail.com> wrote:
> Hi, > > I have to a project which I am converting it to Unicode. I am > getting following error. I am not sure How to fix it. > > error C2664: 'LogFile::LogToFile' : cannot convert parameter 1 from > 'const unsigned short [65]' to 'std::string' > > The code is > #define MAIN_LOG_FILE ".\\ABC.log" > > Defination LogToFile(std::string message, std::string fileName); > > I am calling this LogToFile("My Name", MAIN_LOG_FILE) > > I have tried using "L" and "_T" It is not working. > > Can you please help? You can use something like this: #include <tchar.h> // MS-specific typedef std::basic_string< TCHAR > tstring; LogToFile(tstring const& message, tstring const& fileName); Then, add _T() around string literals where appropriate, e.g: #define MAIN_LOG_FILE _T(".\\ABC.log") LogToFile(_T("My Name"), MAIN_LOG_FILE) This way you can compile both Unicode and non-Unicode versions of your program from a single set of source files. Just define _UNICODE when compiling w/ Unicode support. -- Cheers, Alex -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |
![]() |
|
| Thread Tools | Search this Thread |
| Display Modes | |
|
|