0

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?

  • By which command are you trying to compile this? Please show the full command line. Also, in `RemoveChannels.cpp`, did you include the header file in which `RemoveChannels` is defined? Perhaps it'd be best if you provided a [mre]. – Ted Lyngmo May 16 '22 at 22:32
  • `gcc` is a C compiler, you want `g++` – Goswin von Brederlow May 16 '22 at 22:36
  • The title is wrong. The first error is another. Perhaps because of using the underscore in the name. – 273K May 16 '22 at 22:38
  • Hey @TedLyngmo thanks for your fast responds. I'm using CMake for creating the make file to compile the code. So besides the default CMake flags i use the following extra flags: if (UNIX) add_compile_options( -DUSE_GLEW -fPIC -msse -msse2 -msse3 -mssse3 -msse4 -msse4.1 -msse4.2 -mavx ) endif() set(CMAKE_CXX_STANDARD 14) – Jonas Sorgenfrei May 16 '22 at 22:39
  • @GoswinvonBrederlow `gcc` will detect language (by file extension), but will not link C++ standard library by default, so this is not this issue. – Marek R May 16 '22 at 22:40
  • 1
    @JonasSorgenfrei What if you do `g++ -c path/to/RemoveChannels.cpp -I necessary/include/path`, does that work? – Ted Lyngmo May 16 '22 at 22:41
  • 1
    @TedLyngmo thanks will give it a try tomororw when im back on my linux station. – Jonas Sorgenfrei May 16 '22 at 22:43
  • @273K i don't think that the underscore is the issue, i get the same compiler error if i rename _message to message. – Jonas Sorgenfrei May 16 '22 at 22:46
  • https://github.com/jonassorgenfrei/Nuke-RemoveChannels/blob/master/src/RemoveChannels.cpp Btw here's the full code of the snippet above. – Jonas Sorgenfrei May 16 '22 at 22:47
  • 1
    `// -------------------- Header -------------------- \\` Don't do this. that trailing backslash has special meaning. It means ignore the newline. Line continues onto next line. Get rid of it and you should be good to go. Look it's even effing up the formatting of this comment. – user4581301 May 16 '22 at 22:52
  • 3
    Side note: It's weird smurf like this that make [mre] an absolutely vital tool. If you'd built a MRE and stripped out the comments, you'd have seen the problem magically vanished. – user4581301 May 16 '22 at 22:55
  • Solved! The header comment was producing the error. Thanks @user4581301 But still weired that it worked when removing the this statements aswell as on windows without complaining. – Jonas Sorgenfrei May 16 '22 at 22:57
  • What compiler did you use on Windows? Turns out Visual Studio prints a very helpful warning: https://godbolt.org/z/eEP5bT4Mc – user4581301 May 16 '22 at 23:08
  • And so does GCC with `-Wall`: https://godbolt.org/z/s6TaMjbb6 – user4581301 May 16 '22 at 23:09
  • Found the difference: MSVC does not count the backslash if there's a space after it. GCC does: https://godbolt.org/z/TMz4cda1f . I don't quite think that's kosher on GCC's part, but I'd have to go Standard diving to find exactly what the official ruling is, and frankly I'm not sure where to start looking. – user4581301 May 16 '22 at 23:15
  • 1
    [Looks like GCC is right.](https://eel.is/c++draft/lex#phases-1.2) – user4581301 May 16 '22 at 23:19
  • Awesome, @user4581301 thanks for diving deeper in and letting me/us know the reason. – Jonas Sorgenfrei May 17 '22 at 00:10

1 Answers1

2

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++;
}
user4581301
  • 33,082
  • 7
  • 33
  • 54
  • 1
    I'm amused and impressed to see that the syntax highlighting at SO caught this mistake too. – user4581301 May 16 '22 at 23:11
  • Yeah you are right (shame on me), i should have pasted the whole code before trying to simplify it then i probably would have seen it my self. Vscode and VS Studio didn't give any hint for it in the editor. – Jonas Sorgenfrei May 17 '22 at 00:08