C++ - 包含文件太多? &结构重新定义?

发布于 2024-10-01 05:36:20 字数 911 浏览 2 评论 0原文

我目前正在为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

七堇年 2024-10-08 05:36:20

我假设你以某种方式包含你的头文件两次。你对此有防范吗?每个头文件都应该有这个:

#ifndef TOKEN_H
#define TOKEN_H

[your header file code]

#endif

如果不是这样,请确保您没有在其他地方两次定义令牌。

I assume you're somehow including your header file twice. Do you have a guard against that? Every header file should have this:

#ifndef TOKEN_H
#define TOKEN_H

[your header file code]

#endif

If that's not it, make sure you're not defining token twice somewhere else.

嘿咻 2024-10-08 05:36:20

您可能缺少 包含防护 并进入包含文件递归。

You are probably missing the include guards and getting into include file recursion.

爺獨霸怡葒院 2024-10-08 05:36:20

将此行添加到每个头文件中:

#pragma once

Add this line to every header file:

#pragma once
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文