gpt4 book ai didi

c++ - 缺少类型说明符错误

转载 作者:行者123 更新时间:2023-11-30 02:10:24 26 4
gpt4 key购买 nike

我在使用的每个头文件中包含头文件时遇到问题

#pragma once

文件结构:

Main.cpp  
- includes
Main.h
class definition

Main.h
- includes
"class.h"
"random_header.h"
others ( windows , fstream )
class declaration

Class.h
- includes
"Main.h"
class cApplication{ };

Other headers ( class, random_header ) include Main.h

Main.h 中的类声明错误:

error C2146: syntax error : missing ';' before identifier 'App' ( App is class name )
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

如果有任何帮助/提示,​​我将不胜感激。

Main.h 代码

#pragma once

#pragma comment (lib, "d3d9.lib")
#pragma comment (lib, "d3dx9.lib")

#include <Windows.h>
#include <d3d9.h>
#include <d3dx9.h>

#include <fstream>

using namespace std;

#include "D3Ddefs.h"
#include "cApplication.h"

extern cApplication App; //error

LRESULT CALLBACK WindowProcedure(HWND,UINT,WPARAM,LPARAM);

好吧,还有你问的更多代码,在我看来发布整个东西有点毫无意义(如果不是,请告诉我),所以我只添加这应该是重要的。

main.cpp

#include "Main.h"

cApplication App;

int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPreviousInstance,LPSTR lpcmdline,int nCmdShow)
{ ... }

D3Ddefs.h(完整)

#pragma once

#include "Main.h"

#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ | D3DFVF_DIFFUSE)

struct D3DVERTEX
{
float fX,
fY,
fZ;
DWORD dwColor;
};

cApplication.h

#pragma once

#include "Main.h"

class cApplication
{
private:
...
};

c应用程序.cpp

#include "cApplication.h"


cApplication::cApplication(void)
{ ...
}

最佳答案

您遇到的是循环包含问题。手动将每个#includes 替换为它们包含的文件的内容,按照它们发生的顺序,忽略之前包含的任何文件,您将在 cApplication.cpp 中看到“extern cApplication App;”发生在 cApplication 的声明之上。

请问如何解决?在不知道为什么 cApplication.h 需要Main.h 的情况下无法真正告诉您。如果你能把这种依赖分开,你应该没问题。你想让 cApplication 成为某种 singleton 吗? ,包括 Main.h 以便您可以访问该“App”变量?

关于c++ - 缺少类型说明符错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4597175/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com