积极答复者
Opencv 一个简单的程序编译出错

问题
-
我用opencv编译一opencv个中文论坛上的简单例程出现如下错误:
HighGUI.cpp(11) : error C2664: 'cvLoadImage' : cannot convert parameter 1 from '_TCHAR *' to 'const char *'
主函数参数是这样的int _tmain(int argc, _TCHAR* argv[]),vs2005自动生成的。错误在这一行:if (argc == 2 && (pImg = cvLoadImage(argv[1], 1)) != 0).
求教高手帮忙解决!
- 已移动 Andrew.Wu 2011年5月19日 3:03 (发件人:Visual Studio 相关讨论(Visual Studio 2010以前版本))
答案
-
int _tmain(int argc, _TCHAR* argv[])
-->
int main(int argc, char* argv[])
如果您不想修改main入口函数的话,您需要将TCHAR*转成char*,Unicode编码下下TCHAR为wchar_t,即你需要利用WideCharToMultiByte()来转换。
Visual C++ enthusiast, like network programming and driver development. At present is being engaged in the WinCE/Windows Mobile platform embedded development.- 已标记为答案 Rob Pan 2011年5月24日 9:29
-
你好,
根据您的错误描述,你可能是在编码格式上有些疑问。
_TCHAR* argv[] 以为着参数是Unicode, 而在代码中,你试图用一个Unicode的字符串,来装换为一个ASCII的字符串。你可以尝试在用一个转换方法把TChar* 转换为char*。例如
char* CPublic::THCAR2char(TCHAR* tchStr)
{
int iLen = 2*wcslen(tchStr);//CString,TCHAR汉字算一个字符,因此不用普通计算长度
char* chRtn = new char[iLen+1]
wcstombs(chRtn,tchStr,iLen+1);//转换成功返回为非负值
return chRtn;
}或者你可以修改您的main的定义,使用char*来作为传入参数。
希望我的建议可以对你的疑问有所帮助
Rob Pan [MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- 已标记为答案 Rob Pan 2011年5月24日 9:29
全部回复
-
你好,
我把你的帖子移到了Visual C++论坛,在这边你可以得到更好的回复。
谢谢你的理解与支持。
Andrew Wu [MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
-
int _tmain(int argc, _TCHAR* argv[])
-->
int main(int argc, char* argv[])
如果您不想修改main入口函数的话,您需要将TCHAR*转成char*,Unicode编码下下TCHAR为wchar_t,即你需要利用WideCharToMultiByte()来转换。
Visual C++ enthusiast, like network programming and driver development. At present is being engaged in the WinCE/Windows Mobile platform embedded development.- 已标记为答案 Rob Pan 2011年5月24日 9:29
-
你好,
根据您的错误描述,你可能是在编码格式上有些疑问。
_TCHAR* argv[] 以为着参数是Unicode, 而在代码中,你试图用一个Unicode的字符串,来装换为一个ASCII的字符串。你可以尝试在用一个转换方法把TChar* 转换为char*。例如
char* CPublic::THCAR2char(TCHAR* tchStr)
{
int iLen = 2*wcslen(tchStr);//CString,TCHAR汉字算一个字符,因此不用普通计算长度
char* chRtn = new char[iLen+1]
wcstombs(chRtn,tchStr,iLen+1);//转换成功返回为非负值
return chRtn;
}或者你可以修改您的main的定义,使用char*来作为传入参数。
希望我的建议可以对你的疑问有所帮助
Rob Pan [MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- 已标记为答案 Rob Pan 2011年5月24日 9:29