首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无效预处理令牌错误

无效预处理令牌错误
EN

Stack Overflow用户
提问于 2012-12-24 21:45:53
回答 2查看 8.6K关注 0票数 2

我正在尝试编译一个包含Middlc.h的Args.c文件。Middlc.h未给出以下错误。

代码语言:javascript
复制
enter code here
Middlc.h:23:1: error: pasting "*" and "ArgList" does not give a valid preprocessing token
Middlc.h:24:1: error: pasting "*" and "CandidateList" does not give a valid preprocessing token
Middlc.h:25:1: error: pasting "*" and "Decl" does not give a valid preprocessing token
Middlc.h:26:1: error: pasting "*" and "DeclList" does not give a valid preprocessing token
Middlc.h:27:1: error: pasting "*" and "ExcList" does not give a valid preprocessing token
Middlc.h:28:1: error: pasting "*" and "Field" does not give a valid preprocessing token
Middlc.h:29:1: error: pasting "*" and "FieldList" does not give a valid preprocessing token
Middlc.h:30:1: error: pasting "*" and "Identifier" does not give a valid preprocessing token
Middlc.h:31:1: error: pasting "*" and "IdentifierList" does not give a valid preprocessing token
Middlc.h:32:1: error: pasting "*" and "Interface" does not give a valid preprocessing token
Middlc.h:33:1: error: pasting "*" and "InterfaceList" does not give a valid preprocessing token
Middlc.h:34:1: error: pasting "*" and "Spec" does not give a valid preprocessing token
Middlc.h:35:1: error: pasting "*" and "SymbolTable" does not give a valid preprocessing token
Middlc.h:36:1: error: pasting "*" and "ScopedName" does not give a valid preprocessing token
Middlc.h:37:1: error: pasting "*" and "Type" does not give a valid preprocessing token
Middlc.h:38:1: error: pasting "*" and "TypeList" does not give a valid preprocessing token
Middlc.h:39:1: error: pasting "*" and "FileStack" does not give a valid preprocessing token

即使解决了单个错误,这也是非常有帮助的。我可以在此基础上解决其他问题。

代码如下:

Middlc.h

代码语言:javascript
复制
#ifndef _Middlc_h_
#define _Middlc_h_

/*
 * Main header file for Middlc.
 */

#define PY(x) printf x 

#define new(x) ((x##_t)malloc(sizeof(struct _##x##_t)))
#define TYPE_DECL(x) typedef struct _##x##_t *##x##_t

#include <sys/types.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

typedef enum { False, True } bool_t;

#define MAX_STRING_LENGTH 1024

TYPE_DECL(ArgList);
TYPE_DECL(CandidateList);
TYPE_DECL(Decl);
TYPE_DECL(DeclList);
TYPE_DECL(ExcList);
TYPE_DECL(Field);
TYPE_DECL(FieldList);
TYPE_DECL(Identifier);
TYPE_DECL(IdentifierList);
TYPE_DECL(Interface);
TYPE_DECL(InterfaceList);
TYPE_DECL(Spec);
TYPE_DECL(SymbolTable);
TYPE_DECL(ScopedName);
TYPE_DECL(Type);
TYPE_DECL(TypeList);
TYPE_DECL(FileStack);

#include "FileStack.h"
#include "SymbolTable.h"

#include "Modes.h"
#include "Decls.h"
#include "Types.h"
#include "Args.h"
#include "Candidates.h"
#include "Exceptions.h"
#include "Fields.h"
#include "Identifiers.h"
#include "Interfaces.h"
#include "Specs.h"
#include "ScopedNames.h"
#include "Parser.h"

#include "Globals.h"
#include "Errors.h"
#endif /* _Middlc_h_ */
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-12-24 21:52:40

这条线

代码语言:javascript
复制
#define TYPE_DECL(x) typedef struct _##x##_t *##x##_t

应该是

代码语言:javascript
复制
#define TYPE_DECL(x) typedef struct _##x##_t * x##_t

(无toke粘贴##)。目前,预处理器试图将星号“粘合”到像SymbolTable_t这样的名称上,从而使无法解析的标识符成为SymbolTable_t。如果省略了##,则编译器会将星号作为单独的标记进行解析,从而修复错误。

票数 6
EN

Stack Overflow用户

发布于 2012-12-24 21:52:26

您需要删除*和x之间的##。这就是:

#define TYPE_DECL(x) typedef struct _##x##_t * x##_t

你为什么要做所有这些宏代码当然是另一回事了--如果你以后想用C++编译器编译你的代码(例如,因为出现了一些非常棒的C++代码),那么使用“新”作为宏会发生什么呢?

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14022182

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档