Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get g++ to compile c++11 code with a move constructor?

Tags:

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 
like image 798
Talia Avatar asked Feb 20 '12 21:02

Talia


People also ask

Can G++ be used to compile C code?

gcc is used to compile C program. g++ can compile any . c or . cpp files but they will be treated as C++ files only.

How do I compile with C ++ 11 support?

You need to add the option --std=c++11 (not c+11) to the compiler's command line, which tells the compiler to use the STanDard language version called C++11.

Are move constructor automatically generated?

If a copy constructor, copy-assignment operator, move constructor, move-assignment operator, or destructor is explicitly declared, then: No move constructor is automatically generated. No move-assignment operator is automatically generated.


1 Answers

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.

like image 200
Kerrek SB Avatar answered Sep 18 '22 15:09

Kerrek SB