gpt4 book ai didi

c++ - 编译器错误 : "does not name a type" in macro when compiling with g++

转载 作者:太空狗 更新时间:2023-10-29 20:03:48 25 4
gpt4 key购买 nike

我正在尝试创建一个宏来删除类的复制和移动构造函数。这段代码无法在 GCC 4.8.2 上用“编译。它有什么问题?如果我将所有内容放在一行中,代码就会编译。

#define DISALLOW_COPY_AND_MOVE(ClassName)                   \
ClassName(const ClassName&) KEYWORD_DELETE; \ // Copy constructor
ClassName& operator=(const ClassName&) KEYWORD_DELETE; \ // Copy assignment operator
ClassName(ClassName &&) KEYWORD_DELETE; \ // Move constructor
ClassName& operator=(ClassName&&) KEYWORD_DELETE; // Move assignment operator

第二行不会发出编译器错误,但最后三行会。

KEYWORD_DELETE 只是:

#if defined CPP11
#define KEYWORD_DELETE = delete
#else
#define KEYWORD_DELETE
#endif // defined CPP11

这是输出:

In file included from ..\ObjectSlicing\src\main.cpp:5:0:
..\ObjectSlicing\src\__rm_utilities__/__rm_utilities__.h:26:5: error: stray '\' in program
ClassName& operator=(const ClassName&) KEYWORD_DELETE; \ // Copy assignment operator
^
..\ObjectSlicing\src\__rm_utilities__/__rm_utilities__.h:27:5: error: stray '\' in program
ClassName(ClassName &&) KEYWORD_DELETE; \ // Move constructor
^
In file included from ..\ObjectSlicing\src\__include__/animals:5:0,
from ..\ObjectSlicing\src\main.cpp:6:
..\ObjectSlicing\src\__include__/../animals/cat.h:28:5: error: stray '\' in program
DISALLOW_COPY_AND_MOVE(Cat)
^
In file included from ..\ObjectSlicing\src\__include__/animals:6:0,
from ..\ObjectSlicing\src\main.cpp:6:
..\ObjectSlicing\src\__include__/../animals/dog.h:28:5: error: stray '\' in program
DISALLOW_COPY_AND_MOVE(Dog)
^
In file included from ..\ObjectSlicing\src\main.cpp:5:0:
..\ObjectSlicing\src\__rm_utilities__/__rm_utilities__.h:26:5: error: 'ClassName' does not name a type
ClassName& operator=(const ClassName&) KEYWORD_DELETE; \ // Copy assignment operator
^
..\ObjectSlicing\src\__rm_utilities__/__rm_utilities__.h:27:14: error: expected constructor, destructor, or type conversion before '(' token
ClassName(ClassName &&) KEYWORD_DELETE; \ // Move constructor
^
..\ObjectSlicing\src\__rm_utilities__/__rm_utilities__.h:28:5: error: 'ClassName' does not name a type
ClassName& operator=(ClassName&&) KEYWORD_DELETE; // Move assignment operator
^

最佳答案

您没有发现错误:程序中出现杂散的“\”

插入符号在错误的位置;它应该指向反斜杠:

..\ObjectSlicing\src\__rm_utilities__/__rm_utilities__.h:26:5: error: stray '\' in program
ClassName& operator=(const ClassName&) KEYWORD_DELETE; \ // Copy assignment operator
^ -- here

你不能在反斜杠连续符后面加上 // 注释;如果您想在宏中嵌入注释,您需要使用 /* */ 注释并将它们放在 反斜杠之前。

关于c++ - 编译器错误 : "does not name a type" in macro when compiling with g++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25791566/

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