Search code examples
c++eclipsec++11eclipse-cdtstdarray

Eclipse CDT cannot resolve std:array, std::vector works fine


I am facing a lot of red crosses saying Symbol 'array' could not be resolved, with code that is building fine.

#include <math.h>
#include <array>
#include <sstream>
#include <string>

static std::string printArray(std::array<double, 3> data) {
  std::ostringstream strs;
  strs << "( " << data[0] << " " << data[1] << " " << data[2] << " )";
  return strs.str();
}

static std::string printVector(std::vector<double> data) {
  std::ostringstream strs;
  strs << "( " ;
  for (const auto & value : data )
      strs << value << " ";
  strs << " )";
  return strs.str();
}

The c++11 feature are activated using the -std=c++11 flag under C/C++ General -> Preposcessor Include Path, Macros etc. -> Porvides -> CDT GCC Built-in Compiler Settings as described here or here.

My question is no duplicate, since it works for std::vector and other c++11 features are handled correctly.

The header (#include <array>) can be resolved by pressing F3.

I am using Eclipse the CDT Version: Neon.3 Release (4.6.3).


Solution

  • I had the exact same symptoms that the OP reported:

    • I had previously applied the " -std=c++11" fix to make modern C++ things like "unique_ptr" syntax work.
    • std::vector and other std template things are resolved without errors.
    • I have #include <array> and F3 successfully navigates to the array header file.

    I tried the solution provided by schorsch312, and it worked except the steps presented are not exactly consistent with my Neon version of Eclipse (same version used by OP). Hence, I had to adjust the instructions slightly. Here is what worked for me:

    1. Select Project -> Properties -> C/C++ General -> C/C++ Paths and Symbols -> Symbols
    2. On that page under Languages, select GNU C++
    3. Add...
      • Name: __cplusplus
      • Value: 201103L
    4. Check: Add to all configurations
    5. Apply / OK.

    Select Project -> C/C+ Index -> Rebuild -- if that operation did not automatically trigger.

    Now, eclipse can resolve references to array properly. Note that this solution assumes you have previously resolved the general issue of getting c++11 syntax working.