【发布时间】:2017-04-04 16:37:52
【问题描述】:
在我的程序(wxWidgets,code::blocks)中,我注意到一些我不太理解的行为。如果我这样写我的标题:
#ifndef RECORDTHREAD_H
#define RECORDTHREAD_H
#include <wx/thread.h>
#include <wx/dialog.h>
#include <wx/string.h>
#include "Serial.h"
class RecordTrackDialog;
class RecordThread : public wxThread
{
public:
RecordThread(RecordTrackDialog* parent);
virtual ~RecordThread();
protected:
private:
virtual ExitCode Entry();
Serial m_serial;
};
#endif // RECORDTHREAD_H
(使用#include "Serial.h" 作为最后一个包含语句)一切正常,但是当我像这样更改包含语句时:
#ifndef RECORDTHREAD_H
#define RECORDTHREAD_H
#include "Serial.h"
#include <wx/thread.h>
#include <wx/dialog.h>
#include <wx/string.h>
我收到这样的错误:
||=== Build: Debug in WindowsDgpsGUI (compiler: GNU GCC Compiler) ===|
C:\wxWidgets-3.0.2\include\wx\msw\winundef.h||In function 'HWND__* CreateDialog(HINSTANCE, LPCTSTR, HWND, DLGPROC)':|
C:\wxWidgets-3.0.2\include\wx\msw\winundef.h|38|error: cannot convert 'LPCTSTR {aka const char*}' to 'LPCWSTR {aka const wchar_t*}' for argument '2' to 'HWND__* CreateDialogParamW(HINSTANCE, LPCWSTR, HWND, DLGPROC, LPARAM)'|
C:\wxWidgets-3.0.2\include\wx\msw\winundef.h||In function 'HFONT__* CreateFont(int, int, int, int, int, DWORD, DWORD, DWORD, DWORD, DWORD, DWORD, DWORD, DWORD, LPCTSTR)':|
C:\wxWidgets-3.0.2\include\wx\msw\winundef.h|69|error: cannot convert 'LPCTSTR {aka const char*}' to 'LPCWSTR {aka const wchar_t*}' for argument '14' to 'HFONT__* CreateFontW(int, int, int, int, int, DWORD, DWORD, DWORD, DWORD, DWORD, DWORD, DWORD, DWORD, LPCWSTR)'|
C:\wxWidgets-3.0.2\include\wx\msw\winundef.h||In function 'HWND__* CreateWindow(LPCTSTR, LPCTSTR, DWORD, int, int, int, int, HWND, HMENU, HINSTANCE, LPVOID)':|
C:\wxWidgets-3.0.2\include\wx\msw\winundef.h|94|error: cannot convert 'LPCTSTR {aka const char*}' to 'LPCWSTR {aka const wchar_t*}' for argument '2' to 'HWND__* CreateWindowExW(DWORD, LPCWSTR, LPCWSTR, DWORD, int, int, int, int, HWND, HMENU, HINSTANCE, LPVOID)'|
C:\wxWidgets-3.0.2\include\wx\msw\winundef.h||In function 'HMENU__* LoadMenu(HINSTANCE, LPCTSTR)':|
C:\wxWidgets-3.0.2\include\wx\msw\winundef.h|111|error: cannot convert 'LPCTSTR {aka const char*}' to 'LPCWSTR {aka const wchar_t*}' for argument '2' to 'HMENU__* LoadMenuW(HINSTANCE, LPCWSTR)'|
C:\wxWidgets-3.0.2\include\wx\msw\winundef.h||In function 'HWND__* FindText(LPFINDREPLACE)':|
C:\wxWidgets-3.0.2\include\wx\msw\winundef.h|126|error: cannot convert 'LPFINDREPLACE {aka FINDREPLACEA*}' to 'LPFINDREPLACEW {aka FINDREPLACEW*}' for argument '1' to 'HWND__* FindTextW(LPFINDREPLACEW)'|
C:\wxWidgets-3.0.2\include\wx\msw\winundef.h||In function 'HICON__* LoadIcon(HINSTANCE, LPCTSTR)':|
C:\wxWidgets-3.0.2\include\wx\msw\winundef.h|311|error: cannot convert 'LPCTSTR {aka const char*}' to 'LPCWSTR {aka const wchar_t*}' for argument '2' to 'HICON__* LoadIconW(HINSTANCE, LPCWSTR)'|
C:\wxWidgets-3.0.2\include\wx\msw\winundef.h||In function 'HBITMAP__* LoadBitmap(HINSTANCE, LPCTSTR)':|
C:\wxWidgets-3.0.2\include\wx\msw\winundef.h|324|error: cannot convert 'LPCTSTR {aka const char*}' to 'LPCWSTR {aka const wchar_t*}' for argument '2' to 'HBITMAP__* LoadBitmapW(HINSTANCE, LPCWSTR)'|
||=== Build failed: 7 error(s), 0 warning(s) (0 minute(s), 1 second(s)) ===|
我不太了解这种行为,因为标头是一个线程,错误来自使用该线程的对话框。谁能解释为什么 C++(或 wxWidgets)会这样?
编辑:
Serial.h 的包含
#ifndef SERIAL_H
#define SERIAL_H
#include <windows.h>
//#include <wx/msw/winundef.h>
#include <stdio.h> // necessary for sprintf
#include <string>
这似乎符合 Marco 的评论,但我不能包含该部分...
【问题讨论】:
-
什么是
Serial.h?当您使用双引号时,它应该意味着它是您项目中的一个文件 - 如果您自己的头文件依赖于其他头文件,那么您写的Serial.h不正确。Serial.h应该对自己的包含负责。 -
可能相关:wiki.wxwidgets.org/…
-
向我们展示 Serial.h.
-
表示有一个或多个表头损坏。在正确编写的代码中,顺序应该无关紧要。
-
这意味着
Serial.h中的某些内容依赖于一个或多个wx标头中的声明或定义。Serial.h需要包含它所依赖的那些标头,或者您始终需要确保#include "Serial.h"仅在包含wx标头后才这样做的其他文件。