C++ - 包含文件太多? &结构重新定义?
我目前正在为 XML 编写一个令牌识别器。我将遵循 FSA 的基础来这样做。所以我有一个包含以下代码的头文件...
#define MAX_LENGTH 512
#define MAX_NAME 25
struct token
{
char name[MAX_NAME + 1];
int type;
unsigned char str[MAX_LENGTH + 1];
};
#define TOKEN_TYPES 8
#define SPACES 0
#define NEWLINE 1
#define WORD 2
#define BEGINTAG 3
#define ENDTAG 4
#define EMPTYTAG 5
#define ERROR 6
#define ENDFILE 7
由此我收到错误:
error C2011: 'token' : 'struct' type redefinition
我还在我的 gettoken.cpp 文件中收到另一个奇怪的错误。我实际实施 FSA 的地方。该文件太长,无法显示全部内容。但这样我就收到了错误...
error C1014: too many include files : depth = 1024
这是该 .cpp 文件的部分代码。我只会在其中包含我的进口。
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <string>
#include "Token.h"
using namespace std;
我确信这对我来说通常是愚蠢的。但请帮帮我!谢谢!
I am currently writing a token recognizer for XML. I am going along the basis of FSA's to do so. So I have a Header file that has the following code...
#define MAX_LENGTH 512
#define MAX_NAME 25
struct token
{
char name[MAX_NAME + 1];
int type;
unsigned char str[MAX_LENGTH + 1];
};
#define TOKEN_TYPES 8
#define SPACES 0
#define NEWLINE 1
#define WORD 2
#define BEGINTAG 3
#define ENDTAG 4
#define EMPTYTAG 5
#define ERROR 6
#define ENDFILE 7
With this I am getting the error:
error C2011: 'token' : 'struct' type redefinition
I am also getting another strange error in my gettoken.cpp
file. Where I actually implement the FSA. The File is far to long to display the entire contents. But with this I am getting the error...
error C1014: too many include files : depth = 1024
And here is part of the code for that .cpp file. I will only include my imports in this.
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <string>
#include "Token.h"
using namespace std;
I am sure it is something silly as it usually is for me. But please help me out! Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我假设你以某种方式包含你的头文件两次。你对此有防范吗?每个头文件都应该有这个:
如果不是这样,请确保您没有在其他地方两次定义令牌。
I assume you're somehow including your header file twice. Do you have a guard against that? Every header file should have this:
If that's not it, make sure you're not defining token twice somewhere else.
您可能缺少 包含防护 并进入包含文件递归。
You are probably missing the include guards and getting into include file recursion.
将此行添加到每个头文件中:
Add this line to every header file: