FreeGLUT Tips: Resolve compile error C2664: cannot convert argument 2 from '_TCHAR *[]' to 'char **'
来源:互联网 发布:8031单片机的rom和ram 编辑:程序博客网 时间:2024/06/15 01:50
编译问题:error C2664
在《OpenGL Tutorial: (1) Setting up OpenGL with Visual Studio》一文中,我们为创建一个OpenGL工程做好了准备。接下来我们用这段程序来测试:
#include "stdafx.h"#include <GL\glew.h>#include <GL\freeglut.h>static void keyboard (unsigned char key, int x, int y);static void display (void);int _tmain (int argc, _TCHAR* argv[]){ glutInit (&argc, argv); glutCreateWindow ("GLUT Test"); glutKeyboardFunc (&keyboard); glutDisplayFunc (&display); glutMainLoop (); return 0;}static void keyboard (unsigned char key, int x, int y){ switch (key) { case '\x1B': exit (EXIT_SUCCESS); break; }}static void display (){ glClear (GL_COLOR_BUFFER_BIT); glColor3f (1.0f, 0.0f, 0.0f); glBegin (GL_POLYGON); glVertex2f (-0.5f, -0.5f); glVertex2f (0.5f, -0.5f); glVertex2f (0.5f, 0.5f); glVertex2f (-0.5f, 0.5f); glEnd (); glFlush ();}
不幸的是编译的时候我们遇到了以下编译错误:
error C2664: 'void glutInit_ATEXIT_HACK(int *,char **)' : cannot convert argument 2 from '_TCHAR *[]' to 'char **'
绕开这个问题
显而易见,问题的原因是,glutInit() 这个函数的参数只支持ANSI的’char *’参数,而我们的Win32 Console Application的 int _tmain (int argc, _TCHAR argv[]) 函数传入了一个 ‘_TCHAR *[]’参数。
让我们暂时忘掉程序的国际化问题。只要把 int _tmain (int argc, _TCHAR* argv[]) 这个入口函数改成 int main (int argc, char* argv[]) 即可绕过此问题。
在使用MBCS字符集的情况下解决此问题
现在让我们正面面对此问题,着手解决。
可以在使用MBCS字符集的情况下解决此问题。方法是:
第1步:使用MBCS字符集
更改项目属性 General | Character Set 属性,将其设置为:
Use Multi-Byte Character Set
Screenshot:
第2步:在 stdafx.h 中增加宏定义
在 stdafx.h 中,增加一下宏定义:
#ifdef _MSC_VER // Check if MS Visual C compiler# ifndef _MBCS# define _MBCS // Uses Multi-byte character set# endif# ifdef _UNICODE // Not using Unicode character set# undef _UNICODE # endif# ifdef UNICODE# undef UNICODE # endif#endif
现在再编译,就能通过了。
在使用Unicode字符集的情况下解决此问题
但是MBCS字符集毕竟是过时(obsolete)的,现代C/C++程序在需要支持国际化特性是,大多采用Unicode字符集。所以下面给出使用Unicode字符集的情况下的解决方案。
第1步:使用Unicode字符集
更改项目属性 General | Character Set 属性,将其设置为:
Use Unicode Character Set
Screenshot:
第2步:在 stdafx.h 中增加宏定义
在 stdafx.h 中,增加一下宏定义:
#ifdef _MSC_VER // Check if MS Visual C compiler# ifndef _MBCS# define _MBCS // Uses Multi-byte character set# endif# ifdef _UNICODE // Not using Unicode character set# undef _UNICODE # endif# ifdef UNICODE# undef UNICODE # endif#endif
解决编译警告:warning C4133
此时编译倒是能通过,但是会有一个警告:
warning C4133: 'function' : incompatible types - from '_TCHAR **' to 'char **'
解决此警告的方式:
(待续)
参考文章
- 《Programming OpenGL in C/C++: How To Setup and Get Started》
- 《Unicode and MBCS》
- 《MBCS Support in Visual C++》
- FreeGLUT Tips: Resolve compile error C2664: cannot convert argument 2 from '_TCHAR *[]' to 'char **'
- error C2664: 'MessageBoxW' : cannot convert parameter 2 from 'const char [2]' to 'LPCWSTR'
- 关于VS2005的error C2664: cannot convert parameter 2 from 'const char [2]' to 'LPCWSTR
- 报错 error C2664: 'CreateWindowExA' : cannot convert parameter 2 from 'unsigned short [10]' to 'const char *'
- error C2664: 'MessageBoxW' : cannot convert parameter 2 from 'char [20]' to 'LPCWSTR' 原因
- error C2664: 'MessageBoxW' : cannot convert parameter 2 from 'char [20]' to 'LPCWSTR' 原因
- error C2664: 'MessageBoxW' : cannot convert parameter 2 from 'char [20]' to 'LPCWSTR' 原因
- error C2664:cannot convert parameter 2 from 'char [260]' to 'LPWCH'
- Visual C++:error C2664: 'GetModuleFileNameW' : cannot convert parameter 2 from 'char' to 'LPWCH'
- error C2664: 'CreateWindowExW' : cannot convert parameter 2 from 'const char [16]' to 'LPCWSTR'
- error C2664: 'cvSetMouseCallback' : cannot convert parameter 2 from ....to'CvMouseback'
- error C2664: 'int CListCtrl::InsertColumn(int,LPCTSTR,int,int,int)' : cannot convert parameter 2 from 'const char [7]' to 'LPCTS
- error C2664: 'strcpy' : cannot convert parameter 1 from 'WCHAR [32]' to 'char *'
- error C2664: 'CWnd::MessageBoxW' : cannot convert parameter 1 from 'const char [12]' to 'LPCTSTR'解决方案
- error C2664: 'CWnd::MessageBoxW' : cannot convert parameter 1 from 'const char [12]' to 'LPCTSTR'解决方案
- error C2664: 'TextOutW' : cannot convert parameter 4 from 'const char [5]' to 'LPCWSTR'
- error C2664: 'atoi' : cannot convert parameter 1 from 'CString' to 'const char *'
- VS2008编译错误, error C2664: cannot convert parameter 1 from 'const char [27]' to 'LPCT
- 程序员面试宝典-实践
- 编程练习之一颗二叉树包含另一颗二叉树
- HTML heading
- Opencv读取摄像头数据
- hdu 2138 How many prime numbers
- FreeGLUT Tips: Resolve compile error C2664: cannot convert argument 2 from '_TCHAR *[]' to 'char **'
- Android开机广播android.intent.action.BOOT_COMPLETED
- 使用注解完成事务控制
- 使用反CASE思想,改写SQL
- android的四种对象引用级别:强、弱、软、虚引用
- [UnityShader]Shader上的优化
- JavaScript初学笔记之<执行环境及作用域>
- 散列表
- Nginx 配置文件nginx.conf 详解