22

I can't seem to get g++ to compile c++11 code that uses a move constructor. I keep getting this error:

collin@Serenity:~/Projects/arraylib$ g++ ./t2.cpp
./t2.cpp:10:27: error: expected ‘,’ or ‘...’ before ‘&&’ token
./t2.cpp:10:38: error: invalid constructor; you probably meant ‘Blarg (const Blarg&)’

The program I am writing is quite different from this, but I trimmed it down to the part that seems like it should definitely work, yet still triggers the error:

#include <iostream>

using namespace std;

class Blarg {
    public:
        Blarg () {};
        Blarg (const Blarg& original) {}; /* Copy constructor */
        Blarg (Blarg&& original) {}; /* Move constructor */
};

int main(int argc, char *argv[])
{
    Blarg b;
    return 0;
}

Can anyone tell me what I am doing wrong? Rather, how to fix it?

This is my gcc version:

gcc (Ubuntu/Linaro 4.6.2-14ubuntu2) 4.6.2
Talia
  • 1,400
  • 2
  • 10
  • 33
  • 2
    Note that passing a const reference for a move constructor is pretty useless; realistically that should be `Blarg(Blarg&& original)`. – ildjarn Feb 20 '12 at 22:00
  • Yeah, thanks. I've fixed that in my actual code. I'll fix it above, too. – Talia Feb 22 '12 at 01:12

2 Answers2

41

Say g++ -std=c++0x ./t2.cpp.

While you're at it, you might as well Do It Right and enable all warnings:

g++ -W -Wall -Wextra -pedantic -std=c++0x -o t2 t2.cpp

You really, really shouldn't be compiling with any less, especially if you are going to ask questions about your code on SO :-) Various optimization flags should optionally be considered for the release version, such as -s -O2 -flto -march=native.

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • 2
    "Do It Right", while the man page for -std=c++0x states "This option enables experimental features that may be removed in future" They sure are defensive in those manpages :-) – Captain Giraffe Feb 20 '12 at 22:01
  • Thanks so much for saving me from filtering through the 600+ page manual... I'll just double check that this works the way I intended and then mark this as the answer. – Talia Feb 20 '12 at 22:02
  • 2
    @CaptainGiraffe: To be sure, the dialect option isn't subsumed under "doing it right". That said, `c++0x` will be supported for some time, but from 4.7 onward you can say `c++11`, too. – Kerrek SB Feb 20 '12 at 22:02
  • Ooh, can't wait to put that switch into action. Christmas parcels comes early this year=) – Captain Giraffe Feb 20 '12 at 22:06
  • Unfortunately, even `-Wall -Wextra -Werror` doesn't enable _all_ warnings (see http://stackoverflow.com/questions/4661561 and http://stackoverflow.com/questions/399850) – Philipp Feb 20 '12 at 23:13
  • @Philipp That's fortunate, not unfortunate. For an extreme example: do you really want a warning for *every* variable you declare, ever, anywhere in your program, even the ones implicitly generated by the compiler? (`-Wlarger-than=1`) For a less extreme example: do you really want a warning when you use, for instance, `long long`, when you already specify `-std=c++11` on the command-line to enable new C++11 language features such as that one? (`-Wlong-long`) –  Feb 22 '14 at 13:12
15

You probably forgot to add -std=c++0x to your commandline.

Willem Hengeveld
  • 2,758
  • 23
  • 19
  • Poor thing, same answer time with just as good relevant answer but not as many up votes. I think yours is a more succinct relevant answer! – Fantastic Mr Fox Mar 27 '14 at 06:12