error: expected unqualified-id before ‘{’ token on Linux gcc
error: expected unqualified-id before ‘{’ token on Linux gcc
我在 linux 上使用 gcc (GCC) 7.3.1 20180303 (Red Hat 7.3.1-5) 编译以下代码时收到以下错误消息]没有问题。
...
#include "DDImage/NoIop.h"
static const char* const CLASS = "RemoveChannels";
// -------------------- Header -------------------- \
class RemoveChannels : public NoIop
{
public:
//! Default constructor.
RemoveChannels (Node* node) : NoIop(node)
{
this->_message = "\w+";
this->operation = 1;
}
//! Virtual destructor.
virtual ~RemoveChannels () {}
void _validate(bool) override;
private:
//! Information private for the node.
ChannelSet channels;
std::regex rgx;
const char* _message;
int operation; // 0 = remove, 1 = keep
};
void RemoveChannels::_validate(bool for_real)
{
if (!this->_message) // Fast return if you don't have anything in there.
{
set_out_channels(Mask_None); // Tell Nuke we didn't touch anything.
return;
}
...
}
...
在使用 gcc 编译上述代码时,我在 linux 上收到以下错误消息(在 windows 上它工作正常!)。
编译器错误:
RemoveChannels.cpp:28:1: error: expected unqualified-id before ‘{’ token
{
^
RemoveChannels.cpp:65:6: error: ‘RemoveChannels’ has not been declared
void RemoveChannels::_validate(bool for_real)
^~~~~~~~~~~~~~
/RemoveChannels.cpp: In function ‘void _validate(bool)’:
RemoveChannels.cpp:67:8: error: invalid use of ‘this’ in non-member function
if (!this->_message) // Fast return if you don't have anything in there.
^~~~
...
如果我从实现函数中删除 this-> 并只使用 _message,它可以毫无问题地编译和工作。
任何人都可以向我解释为什么会发生这种情况,并且只发生在 linux 而不是发生在 windows 上吗?
简单示例
// -------------------- Header --------------------\
class RemoveChannels
{
public:
int operation = 0;
};
int main ()
{
RemoveChannels r;
r.operation++;
}
当一行以反斜杠结束时,它会在下一行继续。这意味着 class RemoveChannels
被不小心注释掉了,一行注释泄漏到下一行。
解决方案:删除反斜杠
// -------------------- Header --------------------
class RemoveChannels
{
public:
int operation = 0;
};
int main ()
{
RemoveChannels r;
r.operation++;
}
我在 linux 上使用 gcc (GCC) 7.3.1 20180303 (Red Hat 7.3.1-5) 编译以下代码时收到以下错误消息]没有问题。
...
#include "DDImage/NoIop.h"
static const char* const CLASS = "RemoveChannels";
// -------------------- Header -------------------- \
class RemoveChannels : public NoIop
{
public:
//! Default constructor.
RemoveChannels (Node* node) : NoIop(node)
{
this->_message = "\w+";
this->operation = 1;
}
//! Virtual destructor.
virtual ~RemoveChannels () {}
void _validate(bool) override;
private:
//! Information private for the node.
ChannelSet channels;
std::regex rgx;
const char* _message;
int operation; // 0 = remove, 1 = keep
};
void RemoveChannels::_validate(bool for_real)
{
if (!this->_message) // Fast return if you don't have anything in there.
{
set_out_channels(Mask_None); // Tell Nuke we didn't touch anything.
return;
}
...
}
...
在使用 gcc 编译上述代码时,我在 linux 上收到以下错误消息(在 windows 上它工作正常!)。
编译器错误:
RemoveChannels.cpp:28:1: error: expected unqualified-id before ‘{’ token
{
^
RemoveChannels.cpp:65:6: error: ‘RemoveChannels’ has not been declared
void RemoveChannels::_validate(bool for_real)
^~~~~~~~~~~~~~
/RemoveChannels.cpp: In function ‘void _validate(bool)’:
RemoveChannels.cpp:67:8: error: invalid use of ‘this’ in non-member function
if (!this->_message) // Fast return if you don't have anything in there.
^~~~
...
如果我从实现函数中删除 this-> 并只使用 _message,它可以毫无问题地编译和工作。
任何人都可以向我解释为什么会发生这种情况,并且只发生在 linux 而不是发生在 windows 上吗?
简单示例
// -------------------- Header --------------------\
class RemoveChannels
{
public:
int operation = 0;
};
int main ()
{
RemoveChannels r;
r.operation++;
}
当一行以反斜杠结束时,它会在下一行继续。这意味着 class RemoveChannels
被不小心注释掉了,一行注释泄漏到下一行。
解决方案:删除反斜杠
// -------------------- Header --------------------
class RemoveChannels
{
public:
int operation = 0;
};
int main ()
{
RemoveChannels r;
r.operation++;
}