Home > Net >  error: expected unqualified-id before ‘{’ token on Linux gcc
error: expected unqualified-id before ‘{’ token on Linux gcc

Time:05-17

i get the following error message when trying to compile the following code on linux with gcc (GCC) 7.3.1 20180303 (Red Hat 7.3.1-5) while it works on windows without problems.

...
#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;
  }
  ...
  
}

...

When compiling the above code i get the following error message on linux with gcc (on windows it works fine!).

Compiler error:

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.
        ^~~~
...

If i remove this-> from the implementing function and just use _message it compiles and works without a problem.

Can anyone explain to me why this is happening and just on linux and not on windows?

CodePudding user response:

Simple example

// -------------------- Header --------------------\\
class RemoveChannels
{
public:
  int operation = 0;
};

int main ()
{
    RemoveChannels r;
    r.operation  ;
}

when a line ends in a backslash, it is continued on the next line. That means class RemoveChannels has accidentally been commented out with a line comment leaking into the next line.

Solution: remove the backslash

// -------------------- Header --------------------
class RemoveChannels
{
public:
  int operation = 0;
};

int main ()
{
    RemoveChannels r;
    r.operation  ;
}
  • Related