I am facing a lot of 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).
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:
Select Project -> Properties -> C/C++ General -> C/C++ Paths and Symbols -> Symbols
On that page under Languages, select GNU C++
Add...
Name: __cplusplus
Value: 201103L
Check: Add to all configurations
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.
The problem is that the symbol __cplusplus does not have the value 201103L.
To change this you need to
Edit Project->Preferences->C/C++ General/C/C+ General->Paths and Symbols->GNU C++->Symbols add define __cplusplus and set value to 201103L setzen.
Add under Window->Preferences->C/C++/Build/Settings/Discovery/CDT GCC Build-in Compiler Settings das Argument "-std=c++11" der Command Line hinzufügen
Activate under Project->Preferences->C/C++ General/C/C++ General/Path and Symbols->Providers for all entries the checkbox Use global provider shared between projects
Related
I have problem with converting integer to string in C++ using CodeBlocks and GCC Compiler. I tried with this but it eject error:
#include <iostream>
#include <string>
int main()
{
int clicks = 0;
string code = to_string(clicks); //error: "to_string" was not declared in this scope
}
And also i tried:
#include <iostream>
#include <string>
int main()
{
int clicks = 0;
std::string code = std::to_string(clicks); // error: "to_string" is not memeber of "std"
}
Is there any solution? Pls help
You need to have support for C++11. How I figured that out? I checked the ref here and saw the C++11 icon.
See this answer on how to do this.
It pretty much says to follow these steps:
Go to Toolbar -> Settings -> Compiler
In the "Selected compiler" drop-down menu, make sure "GNU GCC
Compiler" is selected
Below that, select the "compiler settings" tab and then the
"compiler flags" tab underneath
In the list below, make sure the box for "Have g++ follow the C++11
ISO C++ language standard [-std=c++11]" is checked
Click OK to save
You need to use stringstream like so:
stringstream ss;
ss << clicks;
std:string code = ss.str();
Include sstream.
this is the code i am trying to compile, got it from another forum somewhere.
// to_string example
#include <iostream> // std::cout
#include <string> // std::string, std::to_string
int main ()
{
std::string pi = "pi is " + std::to_string(3.1415926);
std::string perfect = std::to_string(1+2+4+7+14) + " is a perfect number";
std::cout << pi << '\n';
std::cout << perfect << '\n';
return 0;
}
I am getting the error:
'to_string' is not a member of 'std'
I have read in other forums to select the flags "Have g++ follow the c++11 ISO language standard [-std=c++11]" and i have and it still doesn't work.
Any help would be greatly appreciated
I am using the GNU GCC Compiler
and code::Blocks 12.11
MinGW-w64 added support for the necessary functionality since GCC 4.8, so make sure you are using at least version 4.8 GCC from MinGW-w64.
You can get one from here, although Code::Blocks should come with a TDM GCC toolchain which should work if it's the latest (because it's GCC 4.8.1 at the time of writing).
i also encounter this error in codeblocks-13.12mingw-setup-TDM-GCC-481.exe(built on 27 Dec 2013).
it seems a bug with tdmgcc4.8.1. maybe the newest tdmgcc whill fixed it.
https://stackoverflow.com/questions/21626866/c-elipse-cdt-getting-to-string-was-not-declared-in-this-scope-with-tdm-gcc-6
the reason of above list should be the same as ours.
==============================
The std::to_string functions are guarded by !defined(_GLIBCXX_HAVE_BROKEN_VSWPRINTF). MinGW defines this symbol as 1 in its os_defines.h, so you know, we cannot use it in mingw.
Easiest way to deal with this error is:
double piValue = 3.1415;
char piBuffer[8];
sprintf(piBuffer, "%f", piValue);
string pi(piBuffer);
I'm using Eclipse 4.2, with CDT, and MinGW toolchain on a Windows machine (although I've a feeling the problem has nothing to do with this specific configuration). The G++ compiler is 4.7
I'm playing with c++11 features, with the following code:
#include <iostream>
#include <iomanip>
#include <memory>
#include <vector>
#include <list>
#include <algorithm>
using namespace std;
int main( int argc, char* argv[] )
{
vector<int> v { 1, 2, 3, 4, 5, 6, 7 };
int x {5};
auto mark = remove_if( v.begin(), v.end(), [x](int n) { return n<x; } );
v.erase( mark, v.end() );
for( int x : v ) { cout << x << ", "; }
cout << endl;
}
Everything is very straight forward and idiomatic c++11. The code compiles with no problems on the command line (g++ -std=c++11 hello.cpp).
In order to make this code compile In eclipse, I set the compiler to support C++11:
Properties -> C/C++ Build -> Settings -> Miscellaneous -> Ohter Flags:
I'm adding -std=c++11
Properties -> C/C++Build -> Discovery Options -> Compiler invocation arguments:
Adding -std=c++11
That's the only change I did to either the global preferences or to the project properties.
First Question: Why do I've to change the flags in two places? When each compiler flags is used?
If I hit Ctrl-B, the project will build successfully, as expected, and running it from within eclipse show the expected result (It prints: '5, 6, 7,').
However, the editor view shows red marks of error on both the 'remove_if' line, and the 'v.erase' line. Similarly, the Problems view shows I've these two problems. Looking at the details of the problem, I get:
For the remove_if line: 'Invalid arguments. Candidates are: #0 remove_if(#0, #0, #1)
For the erase line: 'Invalid arguments Candidates are: '? erase(?), ? erase(?,?)'
Second questions: It appears there are two different builds: one for continues status, and one for the actual build. Is that right? If so, do they have different rule (compilation flags, include paths, etc.)?
Third question: In the problem details I also see: 'Name resolution problem found by the indexer'. I guess this is why the error message are so cryptic. Are those messages coming from MinGW g++ compiler or from Eclipse? What is this Name resolution? How do I fix?
Appreciate your help.
EDIT (in reply to #Eugene): Thank you Eugene. I've opened a bug on Eclipse. I think that C++11 is only partially to blame. I've cleaned my code from C++11 stuff, and removed the -std=c++11 flag from both compilation switch. And yet, the CodAn barks on the remove_if line:
int pred( int n ) { return n < 5; }
int main( int argc, char* argv[] )
{
vector<int> v;
for( int i=0; i<=7; ++i ) {
v.push_back( i );
}
vector<int>::iterator mark = remove_if( v.begin(), v.end(), pred );
v.erase( mark, v.end() );
for( vector<int>::iterator i = v.begin(); i != v.end(); ++i ) {
cout << *i << ", ";
}
cout << endl;
}
The code compiles just fine (with Ctrl-B), but CodAn doesn't like the remove_if line, saying: Invalid Arguments, Candidates are '#0 remove_if(#0,#0,#1)'.
This is a very cryptic message - it appears it misses to substitute arguments in format string (#0 for 'iterator' and #1 for 'predicate'). I'm going to update the bug.
Interestingly, using 'list' instead of 'vector' clears up the error.
However, as for my question, I'm curious about how the CodAn work. Does it uses g++ (with a customized set of flags), or another external tool (lint?), or does it do it internally in Java? If there is a tool, how can I get its command line argument, and its output?
Build/Settings - these flags will be included into your makefile to do actual build. Build/Discovery - these flags will be passed to a compiler when "scanner settings" are discovered by IDE. IDE will run compiler in a special mode to discover values of the predefined macros, include paths, etc.
I believe, the problems you are seeing are detected by "Codan". Codan is a static analysis built into the CDT editor, you may find its settings on "C/C++ General"/"Code Analysis". You should report the problem to the bugs.eclipse.org if you feel the errors shown are bogus. Note that CDT does not yet support all C++11 features.
I'm using _GLIBCXX_DEBUG mode to help find errors in my code but I'm having a problem which I think is an error in the library, but hopefully someone can tell me I'm just doing something wrong. Here is a short example which repro's the problem:
#define _GLIBCXX_DEBUG
#include <iostream>
#include <sstream>
int main (int argc, const char * argv[]) {
std::ostringstream ostr;
ostr << 1.2;
std::cout << "Result: " << ostr.str() << std::endl;
return 0;
}
If I comment out the #define then the output is (as expected):
Result: 1.2
With the _GLIBCXX_DEBUG define in place however the output is simply:
Result:
I've tracked this down to the _M_num_put field of the stream being left as NULL, which causes an exception to be thrown (and caught) in the stream and results in no output for the number. _M_num_put is supposed to be a std::num_put from the locale (I don't claim to understand how that's supposed to work, it's just what I've learned in my searching so far).
I'm running this on a Mac with XCode and have tried it with both "LLVM GCC 4.2" and "Apple LLVM Compiler 3.0" as the compiler with the same results.
I'd appreciate any help in solving this. I want to continue to run with _GLIBCXX_DEBUG mode on my code but this is interfering with that.
Someone else has seen this over at cplusplus.com
and here at stackoverflow, too.
Consensus is that it is a known bug in gcc 4.2 for Mac OS, and since that compiler is no longer being updated, it is unlikely to ever be fixed.
Seems to me that you can either (1) use LLVM, or (2) build your own GCC and use it.
I've a problem with my CDT. Code completion doesn't work for standard library classes.
For example in this code after entering x. and presing ctrl+space IDE doesn't display the list of API elements.
#include
void f() {
string x = "sss";
x.
}
String and vector header files are available in Includes directories. When I press ctrl+click on the include line I'm redirected to header file.
Code completion seems to work fine for C std library.
My version of eclipse:
Eclipse IDE for C/C++ Developers
Version: Helios Release
Build id: 20100617-1415
Eclipse C/C++ Development Tools
Version: 7.0.0.201006141710
Build id: 201006141710
Please help.
Try this:
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string str ("Test string");
cout << "The size of str is " << str.size() << " characters.\n";
return 0;
}
first: check whether it compiles, then place cursor before "return" and try code assist: "str." and see if it pops up.