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.

Go Back   { mindfrost82.com } > Gadget Corner > Tech Newsgroups > Programming > C++

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-01-2008, 05:28 PM
Trups
 
Posts: n/a
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! ]

Reply With Quote
  #2 (permalink)  
Old 08-01-2008, 08:39 PM
ManicQin
 
Posts: n/a
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! ]

Reply With Quote
  #3 (permalink)  
Old 08-01-2008, 08:40 PM
Oncaphillis
 
Posts: n/a
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! ]

Reply With Quote
  #4 (permalink)  
Old 08-11-2008, 12:02 AM
Alex Shulgin
 
Posts: n/a
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! ]

Reply With Quote
Reply

  { mindfrost82.com } > Gadget Corner > Tech Newsgroups > Programming > C++


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT. The time now is 03:58 PM.


Powered by vBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.1.0 ©2007, Crawlability, Inc.
© 1999-2008 mindfrost82.com v11.0


Sponsors:
Buy Anything On eBay | Mortgages | Credit Report | Mortgage Loans | Apply for Credit Card



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114