Is it possible to undefine a class in C++ (Visual Studio)? - c++

I know, it may seem strange, but my goal is to undefine a class in C++. The root of the problem is in combining TinyXML2 and Boost unit tests.
Contents of the header file (Configuration.h) which is being tested:
...
#include <tinyxml2.h>
...
And this is the contents of my configurationTests.h file:
#include "unitTests.h"
#include "Configuration.h"
BOOST_AUTO_TEST_SUITE(configuration_test)
BOOST_AUTO_TEST_CASE(basic) {
...
}
BOOST_AUTO_TEST_SUITE_END( )
When I try to compile my tests, I'm getting an error:
error C2371: 'XMLDocument' : redefinition; different basic
types c:\program files (x86)\windows kits\8.0\include\um\msxml.h 10085
Inside this file (msxml.h) on line 10085 we have this class definition:
class DECLSPEC_UUID("CFC399AF-D876-11d0-9C10-00C04FC99C8E")
XMLDocument;
When I remove those two lines, my tests do compile and everything seems fine. Of course, this is not a solution, but that fact prooves that something inside Boost unit tests library includes msxml.h and somehow leads to conflict with TinyXML2 library.
I tried different solutions found in Google (like writing "#define WIN32_LEAN_AND_MEAN"), removing "using namespace tinyxml2" and making changes inside tinyxml2.cpp - nothing actually helps.
So, my question is simple: can I undefine (unload?) previously defined class in compile time in some tricky way? I tried "#undef XMLDocument", "#define XMLDocument 1" + "#undef XMLDocument" - nothing works.
Update: Actually, I kinda solved the problem by writing "#define MSXML_LIBRARY_DEFINED" on the first line of configurationTests.h. But still, I would love to know an answer to this topic question.

I think you have used default namespace for tinyxml2.
Try to delete using namespace tinyxml2 and then use it like in this example:
tinyxml2::XMLDocument xDoc;

try this:
namespace your_name_space
{
#include <tinyxml2.h>
}
From now all classes inside the tinyxml2 are hidden by your namespace.
The namespece must be declared also for the tinyxml2.cpp file.

Related

There are unresolved includes inside <iostream>

I just tried to compile my C++ code and an error appears when I try to do so.
The error appears on line 9
Here are the versions of the gcc and g++ and such
Any help would be appreciated.
Edit:
I am also including Movie.h:
And also Movie.cpp:
https://puu.sh/vb53G/9e9abd1832.png (I was not able to include more than 3 images due to restrictions)
Firstly, in your Movie.h file, you have not included the string header file correctly. It should be:
#include <string> // without the .h extension
error: 'string' does not name a type
Secondly, you have forgotten to add the closing parenthesis of the constructor function of class "Movie". I am assuming that you have added this now, after the edit
As for the marking done by your compiler, you may find the following StackOverflow post helpful:
StackOverflow Post: Unresolved inclusion iostream.
The link is for the Eclipse IDE, but you can find a similar solution for your own IDE (I cannot tell which one you have).
The line under the #include is just a warning (I'm not sure why).
However, the errors are from the "Movie" class:
1. add "using namespace std" on the top of this class.
2. close the parenthesis on the constructor of 'Movie'.
The error messages are fairly clear:
'string' does not name a type
That is, the compiler is unaware of the type string because either:
you have not #include <string> in Movie.h
or you have, but have not brought it into your namespace with a using namespace std;
although why not just refer to it as std::string?
You are missing
#include <string>

Qt shared_ptr not found when including a library

I'm using Qt creator and the yaml-cpp library. I placed yaml-cpp under my source code directory and added it to the Qt project's include path like so:
INCLUDEPATH += Crypto \
Yaml
QMAKE_CXXFLAGS += -std=gnu++0x
DEFINES += __GXX_EXPERIMENTAL_CXX0X__
As you can see, I also told it to use C++ 11 because that is a requirement of this library. However, I get this error on compiling my project (this is the only error):
../ProjectName/Yaml/yaml-cpp/node/ptr.h:11:10: fatal error: 'boost/shared_ptr.hpp' file not found
#include <boost/shared_ptr.hpp>
^
I also tried, at the advice of some online resources, to replace it with #include <memory>, but this does not work. When I try this, it still cannot find shared_ptr.
I probably could compile the library and link Qt creator to it, but I would have to do this on every platform I use and on every machine that every project developer uses. It seems like a more elegant solution to put the source code inside my GitHub directory and compile it along with the project to minimize the overhead of someone compiling the project on their machine, particularly since this is an open source project.
Here is the source code of Yaml-Cpp's file in question:
#include "yaml-cpp/dll.h"
#include <boost/shared_ptr.hpp>
namespace YAML
{
namespace detail {
class node;
class node_ref;
class node_data;
class memory;
class memory_holder;
typedef boost::shared_ptr<node> shared_node;
typedef boost::shared_ptr<node_ref> shared_node_ref;
typedef boost::shared_ptr<node_data> shared_node_data;
typedef boost::shared_ptr<memory_holder> shared_memory_holder;
typedef boost::shared_ptr<memory> shared_memory;
}
}
It seems that you do not have boost installed. You would need to amend that first.
However, you could urge the yaml-cop developers to use the recent C++ standard more and more when their software is built using C++11 or later. C++11 is not a new thing anymore. It should be utilized as much as possible.

C++ cout gives undeclared identifier

So, I have this question. Why does cout throws
error C2065: 'cout' : undeclared identifier
I am using Visual Studio 2012 as an IDE and I am writing a school project. I have everything done except an example file. So I am trying to write something on the screen like this:
#include "iostream"
#include "stdafx.h"
using namespace std;
int main()
{
cout<<"example";
return 0;
}
So the problem is with cout... printf works fine, but I want to use cout.
EDIT:
I've changed "" to <> but it is not helping. Also I am using this code only for example... This is not the whole project.
stdafx.h shall be the first include directive in your source file.
Switch files and convert the second include to <>, as other suggested.
#include "stdafx.h"
#include <iostream>
See this post for more information.
First of all:
#include <iostream>
instead of #include "iostream"
Secondly, it is generally considered bad practice to write using namespace std;, even though most courses start with that. It is better to only use what you actually need, in your case:
using std::cout;
#include "iostream"
should be
#include <iostream>
Quoting from this post:difference-between-iostream-and-iostream-quotes-in-include
By courtesy of #Jerry Coffin's answer:
When you use < >, the compiler only looks in the system-designated directory/directories (e.g., whatever you've set in the include environment variable) for the header.
When you use " ", the compiler looks in the local directory first, and if that fails, re-searches just like you'd used < >. Technically, (i.e., according to the standard) that doesn't have to be the "local" directory, but that's how it works in essentially every compiler of which I'm aware).
EDIT:
However, the root cause is that stdafx.h is a precompiled header. Visual C++ will not compile anything before the #include "stdafx.h" in the source file, unless the compile option /Yu'stdafx.h' is unchecked (by default); it assumes all code in the source up to and including that line is already compiled. However, it is still better to use <> with iostream not to confuse reader of the code.
If you use #include <iostream> with the <> instead of "" then it should work. Right now, the compiler doesn't know where to find the iostream library.
Also, you might want to change cout<<"example"; to cout<<"example"<<endl; for a new line so that it formats correctly.
Came across this issue while trying to build a Dynamic Linked Library. Make sure that instead of the #include stdafx.h you specify the following include on the first line of your .cpp file:
#include "pch.h"
This should also be the case for VS2017 or earlier.
This error also occurred in the Visual Studio 2017 IDE. Moving stdafx.h to the top solved the error.
For more on stdafx.h, see What's the use for "stdafx.h" in Visual Studio?

How do you include the C++ standard library in xcode 4.3

I'm trying to use C++ code in my ios app (I really don't like objective C, I'm going to use it only when necessary [view control, etc]) and while everything seems to be working I get an error in the following test file.
#ifndef prueba3_GlobalStatic_h
#define prueba3_GlobalStatic_h
#include <string>
using namespace std;
class GlobalStatic
{
public:
GlobalStatic();
~GlobalStatic();
string foo();
private:
int integerValue;
};
#endif
When I try to build the project the IDE gives me the following error:
" 'string' file not found"
I've looked around but cannot find a conclusive answer; any help would be appreciated. In essence, how do I get the standard library working?
One cause of missing c++ headers is including them from an objective-c context as opposed to from objective-c++ -- and you can't use the c++ stl from c! The easier solution is to simply change all your .m files to .mm, .mm will send them to the objective-c++ compiler.

MOC adding namespace to class names

I have this very strange problem while compiling the project.
MOC seems to be adding a namespace to the class name being moc'ed, although it's not mentioned anywhere in the file/class.
The namespace, however, exists in a library which I use, but it's hidden far away in the header files and I don't use it in the UI files. This is what MOC generates:
const QMetaObject SmpTl::CaptureController::staticMetaObject = {
{ &QObject::staticMetaObject, qt_meta_stringdata_SmpTl__CaptureController,
qt_meta_data_SmpTl__CaptureController, 0 }};
The SmpTl namespace is not mentioned anywhere in the declaration of CaptureController, but it appears in the MOC-generated .cpp file.
I'm using Visual Studio with the QT integration.
I also ran into this problem. I had code that looked like this:
namespace foo {
#ifdef _WIN32
... // This code was fine
#else
#error Not Supported
#endif
}
This confused MOC into thinking namespace foo never closed. Apparently, it didn't know _WIN32 was defined, and got tripped up by the fact that I forgot to put quotes around the error message. Changing it to:
#error "Not Supported"
fixed my problem.
SmpTl is the namespace CaptureController is defined in, as it was found by MOC.
The Q_OBJECT macro expands into the declaration of the staticMetaObject-variable inside your class definition (among other things it expands into). The MOC-file contains the definition of that variable.
If this is not correct, please post your Qt version and a stripped down version of your header-file.