5

i am testing std::move with this simple example

but when i tried to compile this code, the error occur

enter image description here

#include <utility>      // std::move
#include <iostream>     // std::cout
#include <vector>       // std::vector
#include <string>       // std::string

int main () {
  std::string foo = "foo-string";
  std::string bar = "bar-string";
  std::vector<std::string> myvector;

  myvector.push_back (foo);                    // copies
  myvector.push_back (std::move(bar));         // moves

  std::cout << "myvector contains:";
  for (std::string& x:myvector) std::cout << ' ' << x;
  std::cout << '\n';

  return 0;
}

but the console window in eclipse seems to say that compile is successful like below

enter image description here

So i tried to compile with console command and not eclipse to check what is correct

g++ -D__GXX_EXPERIMENTAL_CXX0X__ -std=c++11 test.cpp

that was successful and working.

what is problem with eclipse?

i am using eclipse version 3.8

SangminKim
  • 8,358
  • 14
  • 69
  • 125

2 Answers2

8

You're getting errors from Eclipse CDT's Code Analysis, not from your compiler. Code Analysis can be useful, but it can be finicky to set up, and because it involves Eclipse doing its own (limited) C++ parse, it doesn't always agree with the real compiler. (The "Semantic error" in the "Type" column is a giveaway that it's a Code Analysis error. Actual compiler errors are listed as "C/C++ Errors.")

Code Analysis errors are harmless - as you saw, they don't affect compilation. To make Code Analysis work, Eclipse does "discovery" - it invokes GCC itself and parses the output to determine stuff like preprocessor defines, include paths, etc. There's a good chance your Eclipse isn't enabling C++11 when it does discovery. Fixing it depends somewhat on your system and version of Eclipse; here's how I fixed it:

  1. Go under Windows, Preferences, C/C++, Build, Settings.
  2. Under the Discovery tab, select "CDT GCC Built-in Compiler Settings [ Shared ]".
  3. Add -std=c++11 to the end of the "command to get compiler specs."
  4. Click Apply. Click OK.
  5. You may need to reindex after this; see below.

In the newest Eclipse, if you're letting Eclipse manage your C++ settings instead of writing your own makefiles, then as @user3159253 says, you can use the toolchain support instead of manually editing compiler settings.

Here are a couple of other things to try.

  • Make sure you're running the latest Eclipse and Eclipse CDT. C++11 isn't that old, and Eclipse CDT gets major new updates every year, and code analysis in particular is complicated and needs the latest code you can find.
  • It can sometimes help to reindex: right-click on your project, go under Indexing, and choose Rebuild. More drastic approaches to reindexing are available if this doesn't work.
Community
  • 1
  • 1
Josh Kelley
  • 56,064
  • 19
  • 146
  • 246
3

I guess that gcc being invoked by Eclipse doesn't receive -std=c++11 argument. If you have a modern CDT check this page: http://wiki.eclipse.org/CDT/User/NewIn83#Toolchains If not, do as @JoshKelley suggests

user3159253
  • 16,836
  • 3
  • 30
  • 56
  • I think the toolchains option only works for Eclipse-managed projects; at least, I've been unable to get it to work for makefile projects. – Josh Kelley Jun 04 '14 at 19:55
  • Sounds reasonable. If Makefile exists, then all tool chain manipulations are supposed to be performed there. – user3159253 Jun 04 '14 at 21:16