我有一个用C编写的.inc文件.它包含一些带有实现的定义和方法签名.
.inc文件:
#define CONCAT(x,y) x ## y
#define LCONCAT(x,y) CONCAT(x,y)
#define DLIST LCONCAT(LCONCAT(double_,LISTELEMENT),_list)
#define DELETEDFIRST LCONCAT(delete_first_double_,LISTELEMENT)
int DELETEDFIRST (DLIST **first, DLIST **last);
int DELETEDFIRST (DLIST **first, DLIST **last)
{...}
Run Code Online (Sandbox Code Playgroud)
我想把它带到c ++.在我的.h文件中,我有define指令和方法签名(封装在命名空间中).在.cpp我只有方法实现.
.h文件
#define CONCAT(x,y) x ## y
#define LCONCAT(x,y) CONCAT(x,y)
#define DLIST LCONCAT(LCONCAT(double_,LISTELEMENT),_list)
#define DELETEDFIRST LCONCAT(delete_first_double_,LISTELEMENT)
namespace ListFunctions {
int DELETEDFIRST (DLIST **first, DLIST **last);
}
Run Code Online (Sandbox Code Playgroud)
.cpp文件
# include ListFunctions.h
namespace ListFunctions {
int DELETEDFIRST (DLIST **first, DLIST **last) {...}
}
Run Code Online (Sandbox Code Playgroud)
我打算在开发我的模块时使用这些列表函数.在我的module.h中,我定义了一个类型double_ node _list(双端),在我的module.cpp中,我将LISTELEMENT定义为" node ",然后包含ListFunctions.h.但ListFunctions.cpp中的相同内容会导致编译错误:
..\ListFunctions.h(86): error C2065:'double_LISTELEMENT_list' : undeclared identifier
..\ListFunctions.h(86): error C2065: 'first' : undeclared identifier
..\ListFunctions.h(86): error C2065: 'double_LISTELEMENT_list' : undeclared identifier
..\ListFunctions.h(86): error C2065: 'last' : undeclared identifier
..\ListFunctions.h(86): error C2078: too many initializers
Run Code Online (Sandbox Code Playgroud)
后来我想把c风格的实现翻译成c ++.由于我缺乏经验,我想知道其他人是否同意我在做什么.
停止.很明显你知道C,但你不懂C++.在尝试使用它编写代码之前学习C++.它是一种与C语言明显不同的语言,并且尝试编写C++就好像它是"C with stuff"一样会导致代码错误.
第一:你想要的几乎肯定是class带有方法的C++ ,而不是包含在数据类型上运行的函数的命名空间(可能是结构?).
第二:我不确定你在这里定义的所有宏到底是做什么的,但它看起来好像你试图定义一组变量函数.这个概念已经作为模板存在于C++中.不要试图用宏复制它; 你会混淆任何阅读你代码的人.