我正在尝试创建一个宏来删除副本并移动类的构造函数。 此代码无法使用"在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
^
答案 0 :(得分:6)
你没有发现error: stray '\' in program
?
插入符号位于错误的位置;它应该指向反斜杠:
..\ObjectSlicing\src\__rm_utilities__/__rm_utilities__.h:26:5: error: stray '\' in program
ClassName& operator=(const ClassName&) KEYWORD_DELETE; \ // Copy assignment operator
^ -- here
您不能在反斜杠行继续符后添加//
条评论;如果你想在宏中嵌入注释,你需要使用/* */
注释并在反斜杠之前将它们放在中。
答案 1 :(得分:0)
另一个原因可能是: 如果你正在使用类和友元函数,并且如果你在你的朋友函数中调用了那个你在调用该函数之前就没有声明过的类-----喜欢:
#include <iostream>
using namespace std;
//class Second;
class first{
public:
int val1;
void setVal(int tval){
val1=tval;
}
void display(){
cout<<"in first value 1 is:"<<val1<<endl;
}
friend Second sum(first fobj,Second sobj);
};
class Second{
public:
int val2;
void setVal(int tval){
val2=tval;
}
void display(){
cout<<"in second value 2 is:"<<val2<<endl;
}
friend Second sum(first fobj,Second sobj);
};
Second sum(first fobj,Second sobj){
Second rel;
rel.val2=fobj.val1+sobj.val2;
return rel;
}
int main(){
first f1;
Second s1;
f1.setVal(50);
s1.setVal(2);
f1.display();
s1.display();
Second rel=sum(f1,s1);
rel.display();
return 0;
}