Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
1.80/5 (2 votes)
See more:
So I run coding based on 2008 coding in visual 2012. So I havent found any example on how to write it in visual 2012.

The error
gaschedule\gaschedule\childview.cpp(333): error C2664: 'Configuration::ParseFile' : cannot convert parameter 1 from 'wchar_t *' to 'char *'
          Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast




The coding is here.
C#
void CChildView::OnFileOpenConfiguration()
{
    CFileDialog dlg( TRUE, NULL, NULL, 0,
         ("Class Schedule Config Files (*.cfg)|*.cfg|All Files (*.*)|*.*|"), this );

    if( dlg.DoModal() == IDOK )
    {
        Configuration::GetInstance().ParseFile(dlg.GetFileName().GetBuffer() );

        ComputeScrollBars();
        Invalidate();
    }
}

C++

Posted

The problem you are having is probably using Multi-Byte characters in a Unicode build. If that is the issue, you may be able to solve it by setting your project to use Multi-Byte characters instead. (I believe MFC will switch between the types appropriately, but I'm not sure because I've never used MFC.)

That isn't the recommended solution, though. It would be better to rewrite it as Unicode, specifically, this line:

CFileDialog dlg( TRUE, NULL, NULL, 0,
     (_T("Class Schedule Config Files (*.cfg)|*.cfg|All Files (*.*)|*.*|")), this );


That should fix it. Unicode is recommended because that is what Windows uses under the hood, and no conversions will be required.

Another approach, if you really want to force it in (which isn't suggested either) is to use WideCharToMultiByte to create another string from the one I placed the "_T" in front of. You would have to set up a variable, and do more work. But again, that wouldn't be a recommendation - it's just for knowledge purposes.

Happy coding!
David
 
Share this answer
 
Comments
[no name] 12-Jun-14 23:10pm    
5+.
Sergey Alexandrovich Kryukov 13-Jun-14 0:28am    
5ed.
—SA
Member 10446157 23-Jun-14 0:08am    
thank you it helps a lot. :D
Member 10446157 23-Jun-14 0:54am    
hello. can you help me?
after I add the _T, it got problems with the next line. in dlg part.

void CChildView::OnFileOpenConfiguration()
{
CFileDialog dlg( TRUE, NULL, NULL, 0,
(_T("Class Schedule Config Files (*.cfg)|*.cfg|All Files (*.*)|*.*|")), this );

if( dlg.DoModal() == IDOK )
{
Configuration::GetInstance().ParseFile(dlg.GetFileName().GetBuffer() );

ComputeScrollBars();
Invalidate();
}
}

the error is below:

c:\users\zahrina\documents\visual studio 2012\projects\gaschedule\gaschedule\childview.cpp(336): error C2664: 'Configuration::ParseFile' : cannot convert parameter 1 from 'wchar_t *' to 'char *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
David O'Neil 23-Jun-14 11:31am    
You now have CFileDialog set up correctly to use wchar_t under the hood for your Unicode build, but you (or someone else) has programmed ParseFile to use a char array instead of a wchar_t array for the filename passed to it. The proper way to solve that would be to change ParseFile to take 'TCHAR *' for its argument, not 'char *', and then modify ParseFile appropriately. The link given by Sergey should help you understand what is going on under the hood better, but basically if you are building a Unicode build, TCHAR is a macro that expands to wchar_t, and if you do a Multi-byte build, TCHAR expands to char. If you use TCHAR everywhere, your program can be built for Unicode or Multi-byte without any problems (usually).
In addition to Solution 1, on "_T" generic types: this CodeProject article provides a good explanation: What are TCHAR, WCHAR, LPSTR, LPWSTR, LPCTSTR (etc.)?[^].

—SA
 
Share this answer
 
Comments
Member 10446157 23-Jun-14 0:09am    
thank you! :D

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900