cannot convert from 'const char to 'ATL::CStringT<BaseType,StringTraits>' error

In my Windows mobile MFC dialog based app I am trying to define a directory like so:

#define      DBF_DIR            "\\Program Files\\MyProgram\\DB"

This is included in the header file MyProgram.h

I get an error message when I try to build this like so:
error C2440: 'initializing' : cannot convert from 'const char [27]' to 'ATL::CStringT<BaseType,StringTraits>'      

How do I solve this??      
Wanting2LearnManAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mrjoltcolaCommented:
Can you post code to show where you are actually using DBF_DIR?  This error is not due to your #define above, but due to how you are using it.
0
jkrCommented:
You might have your charset set to UNICODE. Does one of the following help?
#define      DBF_DIR            L"\\Program Files\\MyProgram\\DB" // plain UNICODE
 
#define      DBF_DIR            _T("\\Program Files\\MyProgram\\DB") // should work for both UNICODE and ANSI

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
evilrixSenior Software Engineer (Avast)Commented:
>> error C2440: 'initializing' : cannot convert from 'const char [27]' to 'ATL::CStringT<BaseType,StringTraits>'      

Try changing it to this...

#define      DBF_DIR            _T("\\Program Files\\MyProgram\\DB") // Wide or narrow depending upon compile time params

or this

#define      DBF_DIR            L"\\Program Files\\MyProgram\\DB" // Wide string

>> CStringT
I'm not 100% but I'm guessing the T at the end means the same as the T in a TCHAR type (it can be either wide or narrow).
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

Wanting2LearnManAuthor Commented:
I have did this:
#define      DBF_DIR            _T("\\Program Files\\MyProgram\\DB")

and it compiles but then when I go to use it like so:
sprintf(szDbFile, "%s\\%s.dbf", DBF_DIR, CONFIG_DB_FILE);

the DBF_DIR apears as an empty string.
0
evilrixSenior Software Engineer (Avast)Commented:
Try: _stprintf(szDbFile, "%s\\%s.dbf", DBF_DIR, CONFIG_DB_FILE);
http://msdn.microsoft.com/en-us/library/ybk95axf(VS.80).aspx
0
mrjoltcolaCommented:
You might also consider using CString, which has a Format method that is similar to sprintf

CString s;
s.Format("%s\\%s.dbf", DBF_DIR, CONFIG_DB_FILE);
0
mrjoltcolaCommented:
Actuall that would be

CString s;
s.Format(_T("%s\\%s.dbf"), DBF_DIR, CONFIG_DB_FILE);
0
Wanting2LearnManAuthor Commented:
re: _stprintf(szDbFile, "%s\\%s.dbf", DBF_DIR, CONFIG_DB_FILE);
gives the error:
error C2664: 'swprintf' : cannot convert parameter 1 from 'char [260]' to 'wchar_t *'

re: Use CString
I have to use:
char szDbFile[MAX_PATH];
sprintf(szDbFile, "%s\\%s.dbf", DBF_DIR, CONFIG_DB_FILE);

as I am using codebase and it needs it to be in that format, i.e. the next line is:
m_pD4ConfigData = d4open(&m_c4CodeBase, szDbFile);      
0
evilrixSenior Software Engineer (Avast)Commented:
>> error C2664: 'swprintf' : cannot convert parameter 1 from 'char [260]' to 'wchar_t *'

Oops, I also forgot to put T() around the format string :)

_stprintf(szDbFile, _T("%s\\%s.dbf"), DBF_DIR, CONFIG_DB_FILE);

szDbFile needs to be declared thus...

TCHAR szDbFile[260];

0
mrjoltcolaCommented:
You can use CStringA (as opposed to CString) and easily convert to a char * for the d4open() method.

It gets hairy mixing Unicode and the T macros with ANSI APIs. The macros work well, but an ANSI API expects ANSI, period, and are usually compiled to expect char *.

I will often just use a CString, for general use, that way all MFC APIs are happy, but when I need to call an ANSI/traditional API I assign to a CStringA for a conversion, and cast that to a char *. Its either that or use the conversion APIs (WideCharToMultiByte....), and I find them too wordy for general use. Or maybe I am yet to learn a better way.



0
evilrixSenior Software Engineer (Avast)Commented:
>> Or maybe I am yet to learn a better way.
UTF8 :)
0
mrjoltcolaCommented:
>>UTF8 :)

What do you mean? So I have a UNICODE app, all t* types and CString are wide-char. So what is the quickest, simplest way to convert for 8-bit APIs? UTF8 is great, but I am talking wide-chars here, right? What did I miss?
0
evilrixSenior Software Engineer (Avast)Commented:
>> What do you mean? So I have a UNICODE app, all t* types and CString are wide-char. So what is the quickest, simplest way to convert for 8-bit APIs?
Just use UTF8 all the time, forget wide... it's not portable and causes so many headaches. Wide for Windows is UTF16 and for Linux it's UTF32 but both will happily work with UTF8.

>> UTF8 is great, but I am talking wide-chars here, right? What did I miss?
I know and I'm saying avoid the pain just use UTF8 rather than UTF16 or UTF32.

It was a bit of a glib and OT comment... sorry ;)
0
evilrixSenior Software Engineer (Avast)Commented:
BTW when you convert to narrow using WideCharToMultiByte you are just converting UTF16 to UTF8 (hence it being multi-byte), you realise that right? Hence I glibly say, just use UTF8 all the time.
0
evilrixSenior Software Engineer (Avast)Commented:
^^^assuming you chose UTF8 as the charset to convert to of course.

I'm rambling now, I think I need to go and sleep :)
0
mrjoltcolaCommented:
Now that we have successfully hijacked another thread.. :)

What I am saying is, assuming we are using MFC with UNICODE defined, we don't necessarily have control? _T("foo") is going to be 16-bit, not 8-bit. TCHAR is wide, LPCTSTR is wide. All of the MFC APIs that expect either CString or LPCTSTR, etc. want wide char stuff, which is not UTF8. I don't want to use the conversion routines, I just want smart strings.

So if it were up to me, I'd use UTF8, but is it really up to me?
0
evilrixSenior Software Engineer (Avast)Commented:
>> Now that we have successfully hijacked another thread.
You're quite right we seem to have hi-jacked a thread, so I relinquish my side of it ;)

0
mrjoltcolaCommented:
@Wanting2LearnMan : did you resolve your problem?
0
Wanting2LearnManAuthor Commented:
Yes this solved my problem
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Smartphone Programming

From novice to tech pro — start learning today.