我知道错误"预期'}'在输入结束之前"意思是,我遇到的问题是错误似乎出现在d3dx11.h中包含的一个文件中,因为当我没有包含此文件并在我的代码中注释掉DirectX调用时我不会#39; t得到错误。
当我搜索这个错误时,我得到了结果,但是我发现的所有结果都是用户代码中的错误(附加或缺少大括号),我的错误似乎发生在包含的文件中,所以6小时后我没有&找到了任何帮助。
我对C ++比较陌生,此时我只是想尝试并理解事情是如何运作的,所以我并不太关心命名约定或我可能会怎么做根据标准做法放入应该进入相应cpp文件的头文件。我的代码可能不是最干净的,并且可能有一些效率低下的问题,请随意指出我可以改进的地方,但对于这个问题,我主要问的是导致错误的原因:
C:/Program Files/CodeLite/.../main.cpp:33:1: error: expected '}' at end of input
正如我之前所说,在我包含DirectX之前,这个错误不会发生。
的main.cpp
#ifndef INCL_WINDOWS_H
#include <windows.h>
#include <windowsx.h>
#define INCL_WINDOWS_H
#endif
#include <iostream>
#ifdef _MSC_VER
# pragma comment(linker, "/subsystem:windows /ENTRY:mainCRTStartup")
#endif
#define OS_WINDOWS
#include "WindowUtils.h"
#include "RenderUtils_Creation.h"
using namespace std;
int main()
{
Window window("Test Window",100,100,900,900);
while(window.getAlive())
{
window.process();
}
return window.getExitCode();
} // <-- Line 33, error occurs here, only when DX is included in RenderUtils_Creation.h
WindowUtils.h
#ifndef INCL_WINDOW_UTILS
#define INCL_WINDOW_UTILS
enum DisplayMode // Currently Unused (TBI)
{
WINDOWED,
FULLSCREEN,
WINDOWED_NOBORDER
};
// DEFINE TEMPLATE FOR IMPLEMENTATION FEATURES
class ImplementationFeatures;
// DEFINE PLATFORM SPECIFIC IMPLEMENTATION OF IMPLEMENTATION FEATURES
#ifdef OS_WINDOWS
class ImplementationFeatures
{
public:
static LRESULT CALLBACK WinProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
switch(msg)
{
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}
HINSTANCE procInst;
WNDCLASSEX wndStruct;
HWND windowHandle;
MSG msg;
};
#endif
// DEFINE TEMPLATE FOR WINDOW
class Window
{
public:
ImplementationFeatures features;
Window(char*, int, int, int, int);
bool getAlive();
void setDisplayMode(DisplayMode);
void process();
void update();
int getExitCode();
};
// DEFINE PLATFORM-SPECIFIC IMPLEMENTATIONS OF WINDOW
#ifdef OS_WINDOWS
Window::Window(char* title, int xPos, int yPos, int width, int height)
{
ImplementationFeatures feature;
LPCTSTR strTitle = title;
feature.procInst = GetModuleHandle(NULL);
feature.wndStruct.cbSize = sizeof(WNDCLASSEX);
feature.wndStruct.style = CS_HREDRAW | CS_VREDRAW;
feature.wndStruct.lpfnWndProc = feature.WinProc;
feature.wndStruct.cbClsExtra = 0;
feature.wndStruct.cbWndExtra = 0;
feature.wndStruct.hInstance = GetModuleHandle(NULL);
feature.wndStruct.hIcon = NULL;
feature.wndStruct.hCursor = NULL;
feature.wndStruct.hbrBackground = GetSysColorBrush(COLOR_BTNFACE);
feature.wndStruct.lpszMenuName = NULL;
feature.wndStruct.lpszClassName = "WindowClassName";
feature.wndStruct.hIconSm = LoadIcon(NULL,IDI_APPLICATION);
RegisterClassEx(&(feature.wndStruct));
feature.windowHandle = CreateWindowEx(WS_EX_CONTROLPARENT, "WindowClassName", strTitle, WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX | WS_VISIBLE, xPos, yPos, width, height, NULL, NULL, GetModuleHandle(NULL), NULL);
ShowWindow(feature.windowHandle, 1);
UpdateWindow(feature.windowHandle);
features = feature;
}
bool Window::getAlive()
{
return GetMessage(&features.msg, NULL, 0, 0) > 0;
}
void Window::process()
{
TranslateMessage(&(features.msg));
DispatchMessage(&(features.msg));
}
void Window::update()
{
UpdateWindow(features.windowHandle);
}
void Window::setDisplayMode(DisplayMode mode)
{
switch(mode)
{
case WINDOWED:
ShowWindow(features.windowHandle,1);
break;
case WINDOWED_NOBORDER:
ShowWindow(features.windowHandle,1);
break;
case FULLSCREEN:
ShowWindow(features.windowHandle,1);
break;
}
}
int Window::getExitCode()
{
return features.msg.wParam;
}
#endif // End OS_WINDOWS
#endif // End INCL_WINDOW_UTILS
RenderUtils_Creation.h
#ifndef INCL_RENDER_UTILS_CRTN
#define INCL_RENDER_UTILS_CRTN
//This won't be included on the main.cpp because of the header guard
//Bug is included in case of use on other files
#include "WindowUtils.h"
#ifdef OS_WINDOWS
// Use Direct3D
//#define SKIP_DIRX // To quickly go back and forth testing if error is DX related
// When commented out d3d11.h and d3dx11.h are included with their libs
// When not commented none are included
// The error only occurs when this is commented.
#ifndef SKIP_DIRX
// Import Headers
#include <d3d11.h>
#include <d3dx11.h>
// include the Direct3D Library files
#pragma comment (lib, "d3d11.lib")
#pragma comment (lib, "d3dx11.lib")
#endif
#endif // End of OS_WINDOWS
#endif // End of INCL_RENDER_UTILS_CRTN
main.cpp是唯一的.cpp文件,没有标题。我试图创建一个跨平台库,用于连接不同的操作系统和图形API。通过定义OS_WINDOWS,DirectX将是针对Mac编译的API,对于Mac,我会添加代码以连接到Metal等。我使用这个抽象,我在过去做过类似的Java,但是这个错误使我感到困惑。
修改 - 可能很重要的其他信息:
从进一步的研究看来,至少部分问题可能是因为微软为他们的C ++编译器开发了自己的标准。 VisualC ++编译器添加了自定义运算符,内部函数,宏和其他标准函数的替代版本,这些函数在其他编译器中不存在,因为它们不是C ++标准,它们是特定于Microsoft的自定义添加项。 DirectX头文件显然依赖于这些功能的存在,因此Microsoft基本上强迫任何使用DirectX的人使用他们的编译器,至少对于他们程序的某些部分。