I've just installed Eclipse and the CDT (C++) plugin, and I'm having the strangest problem.
From a fresh C++ Project, the automatically generated "Hello World!" program works just fine. However, upon creating a new C++ Class in the very same project, namespace std is not recognized. I invoke the namespace with the same syntax in both files, but the compiler is throwing this error -
"error: use of undeclared identifier 'std'"
I've restarted both Eclipse and my computer, but the problem persists. So far as I can tell, the properties of both files are exactly the same.
I suspected this had something to do with the tool chain, but it is the same in both of the files. I really can't find any difference between the two files, other than their size and date created.
Your program probably didn't #include any headers that define the namespace std. Please add the appropriate #include files that define the std namespace.
Related
Why when I use getch() function in c++, I should include "conio.h" header file ... while in c it runs without include this file?
I'm using codeBlocks as my IDE.
I expect that I must include "conio.h" in c program also, and I tried to include "stdio.h" and "stdlib.h" in c++ program but there is no result.
Why when I use getch() function in c++, I should include "conio.h" header file ...
C++ requires that identifiers be declared before they are used.
… while in c it runs without include this file?
You are using an implementation of an old version of C in which a function call with an undeclared identifier defaulted to treating the identifier as one for a function with undeclared parameters with return type int.
I'm using codeBlocks as my IDE.
The IDE is irrelevant, except that there are some associations between some IDEs and some sets of development tools. The critical information is the name and version number of the compiler and the switches you are giving to it. Saying what IDE you are using is like saying what picture frame you are using in a question about a picture. The frame does not identify the picture, and the IDE does not identify the compiler you are using inside the IDE.
I am testing with a simple test program right now. It looks like below:
#include <iostream>
using namespace std;
int main() {
string s = "a b c d ";
remove(s.begin(),s.end(),' ');
}
When i build it with visual studio, it builds correctly and does not give any error. However if i try to build it with eclipse (mingw), it complains about the functions 'remove', as it should because the corresponding header is not included.
Is there a way to configure visual studio such that it will also complain and not auto-include headers or whatever fancy thing it is doing? I have already checked by disabling the option to use pre-compiled headers in visual studio project properties, and that doesn't help.
When you write a program that fails to include the proper headers, some toolchains may still just so happen to successfully build your program, because maybe their <iostream> happens to ultimately include the header you need (like <algorithm>).
That doesn't change the fact that your code is wrong. You're getting a build by chance.
You don't configure another toolchain to do that. You fix your code to include the correct headers.
So:
#include <string>
#include <algorithm>
The C++ Standard does not define, that a certain file needs to be included for the contained definitions to be able to be used.
It only defines in which files the specific functions are defined.
So if the specific implementation which You use includes everything through a file and You don't need to include anything else, than that is still allowed by the Standard.
So in one implementation, everything will compile, while in another errors will appear.
This is not controlled by the C++ Standard.
What You can do is file a bug to the implementors, and see if they agree that it's a bug. (In this case: https://github.com/microsoft/stl/issues)
I'm trying to include a namespace from another project in my project but I get the "symbol could not be resolved" error.
using namespace project;
^ This line gives the "symbol could not be resolved" error.
I have the files with this namespace included in my project.
I'm using eclipse on ubuntu Mate.
The C++ compiler does not know about the project management of your IDE (development environment).
It only sees the source code itself. If you add an #include statement the compiler will also see the included code as if it has been put at that place. #include is recursive, so if one included file includes another that will be seen as well.
The error message means that at the point of your using namespace project the symbol project is not known. So, you obviously miss an earlier #include statement including any header of the other project which contains a namespace project { ... } defining the namespace.
Any implementation (.cpp) file using namespace project needs such an #include statement.
To use a namespace of another project, u have to refer that project in your existing project.
In eclipse, you can add the project by following below steps.
Right click > Properties > C/C++ General > Paths and Symbols
Go to Tab Library and Add new library path
You can see the referenced path, check the project and Apply.
You can now use the namespace.
This Link can give more info about references and other settings of the project in eclipse.
I've been trying to follow lazy foo's productions tutorial on sdl2 and I keep on running into the same issue. I made a template that links to the correct files and all and it worked for a while. But now when I create a project and include iostream for example, it tells me #using need c++/cli mode enabled.
So I then tried enabling it in the project settings but then it gave another error : "Cannot open metadata file iostream"
I tried :
Rebuilding the project and solution
Cleaning the project and solution
I read this question and its answers : Visual studio - getting error "Metadata file 'XYZ' could not be found" after edit continue
Tried this too : IntelliSense: "#using" requires C++/CLI to be enabled
All of the above did not work
Don't confuse #include, using and #using.
#using is used to import class libraries in C++/CLI, which is something you won't ever need unless you work with .NET libraries (but then usually you are better off just using C#, unless you are writing interop code).
#include is for including header files, which is what you normally do in "regular" C++. <iostream> is a regular standard library header, so you need #include (as in #include <iostream>).
using instead is used to bring names in the current scope (either the whole content of a namespace - as in the dreaded using namespace std;) or single names (as in using std::cout;). From C++11 it's also used to enable constructors inheritance and to create type aliases, but for the moment I don't think you need to worry about these uses.
But most importantly: please take the time to first learn the basics of the language from reputable sources before trying random stuff. All this #using mess wouldn't have arisen if you even just looked first at the classic hello would example you can find everywhere on the Internet.
My previous questions concerning the same project: one and two. It's not necessary to read them; just know that I am trying to use a native C++ SDK in a Visual C++ project. This is much trickier than I had initially thought, but this website about Extending a native C++ project with managed code has helped me a lot further already.
As per that last link's instructions, I have added a Form to my native C++ project, which has automatically converted the project to a CLR one. Only MainForm.cpp and Interface.cpp (the file that allows native C++ code to create and show a MainForm) are compiled with the /clr flag though; the other files remain native.
The problem I have now, is that Visual Studio doesn't seem to recognise any of the CLR stuff that's being used in MainForm.h. As such, in the following lines:
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
the word System is always underlined in red, with according errors:
error C2653: 'System' is not a class or a namespace name
for each of those lines.
It also does not recognise the word gcnew and other things that should work effortlessly inside CLR.
Can anybody tell me what I might be doing wrong? My guess is that it's something very small; some flag I have forgotten to change, a missing reference or something similar, but I just can't figure out what it is.
Where do you include MainForm.h - directly and indirectly?
If you include MainForm.h in Interface.h, you include it indirectly anywhere where you include Interface.h as well. That means, if you then include Interface.h in any translation unit (i.e. *.cpp) that is not compiled with /clr, then the compiler will of course complain about it, because namespace System and gcnew are not part of standard C++.
Therefore you should include MainForm.h only in Interface.cpp and use forward declarations in Interface.h.