在对话框中创建了3个文本框,从第一个和第二个文本框读取数据然后把它们的和在第3个文本框中显示(孙鑫vc++深入详解第7章中的例子)
在编译时总是出现类型不能转换,但如果按照出错提示进行强制转换则可以运行,是不是vs2005下mfc的编程没法进行隐式转换还是哪里没设置好啊?
我以前在vc6.0下编的时候好象都能自动转换的,不会提示出错,顶多是warning,请高手指教啊void CTestDlg::OnBnClickedBtnAdd()
{

int num1,num2,num3;
char * ch1[10],ch2[10],ch3[10];
GetDlgItem(IDC_EDIT1)->GetWindowText(ch1,10);
GetDlgItem(IDC_EDIT2)->GetWindowText(ch2,10);
GetDlgItem(IDC_EDIT2)->SetWindowText(ch1);
num1=atoi(ch1);
GetDlgItem(IDC_EDIT3)->SetWindowTextW(ch1);
num2=atoi(ch2);
num3=num1+num2;
_itoa_s(num3,ch3,10,10);
GetDlgItem(IDC_EDIT3)->SetWindowTextW(ch3); // TODO: 在此添加控件通知处理程序代码
}/*
1>------ 已启动生成: 项目: Mybole, 配置: Debug Win32 ------
1>正在编译...
1>TestDlg.cpp
1>e:\study\visual c++深入详解\chapter7\mybole\mybole\testdlg.cpp(55) : error C2664: “int CWnd::GetWindowTextW(LPTSTR,int) const”: 不能将参数 1 从“char *[10]”转换为“LPTSTR”
1>        与指向的类型无关;转换要求 reinterpret_cast、C 样式转换或函数样式转换
1>e:\study\visual c++深入详解\chapter7\mybole\mybole\testdlg.cpp(56) : error C2664: “int CWnd::GetWindowTextW(LPTSTR,int) const”: 不能将参数 1 从“char [10]”转换为“LPTSTR”
1>        与指向的类型无关;转换要求 reinterpret_cast、C 样式转换或函数样式转换
1>e:\study\visual c++深入详解\chapter7\mybole\mybole\testdlg.cpp(57) : error C2664: “CWnd::SetWindowTextW”: 不能将参数 1 从“char *[10]”转换为“LPCTSTR”
1>        与指向的类型无关;转换要求 reinterpret_cast、C 样式转换或函数样式转换
1>e:\study\visual c++深入详解\chapter7\mybole\mybole\testdlg.cpp(58) : error C2664: “atoi”: 不能将参数 1 从“char *[10]”转换为“const char *”
1>        与指向的类型无关;转换要求 reinterpret_cast、C 样式转换或函数样式转换
1>e:\study\visual c++深入详解\chapter7\mybole\mybole\testdlg.cpp(60) : error C2440: “<function-style-cast>”: 无法从“char *[10]”转换为“CString”
1>        无构造函数可以接受源类型,或构造函数重载决策不明确
*/

解决方案 »

  1.   

    应该是的。.NET要提高安全性阿。
      

  2.   

    char -> TCHAR
    _itoa_s -> itow_s
      

  3.   

    不是不进行隐式转换,是隐式转换失败。
    2005中创建项目默认使用Unicode字符串,所以char*没法转换为LPTSTR或者LPCTSTR,最好是把char改为TCHAR,也可以改为WCHAR。
      

  4.   

    可以在项目设置中,把使用Unicode字符集改为使用多字节字符集。
      

  5.   

    换成TCHAR后GetWindowText和SetWindowText的参数转换没问题了,
    但atoi参数的转换问题又出来了1>TestDlg.cpp
    1>e:\study\visual c++深入详解\chapter7\mybole\mybole\testdlg.cpp(57) : error C2664: “atoi”: 不能将参数 1 从“TCHAR [10]”转换为“const char *”
    1>        与指向的类型无关;转换要求 reinterpret_cast、C 样式转换或函数样式转换
    1>e:\study\visual c++深入详解\chapter7\mybole\mybole\testdlg.cpp(59) : error C2664: “atoi”: 不能将参数 1 从“TCHAR [10]”转换为“const char *”
    1>        与指向的类型无关;转换要求 reinterpret_cast、C 样式转换或函数样式转换如果我进行强制转换的话能正常运行,但是在文本框里输入两位数时,每次读取到的只是第一位,比如说输入35,64,则加出来的结果为9,是3和6的和,这什么原因啊
      

  6.   


    改了后貌似还是不能从CHAR *转化为LPTSTR啊?