removing #include causes error - c++

I have a class called D3DGraphics in a header file called D3DGraphics.h. I have included d3d9.h and my Graphics file works absolutely file.
However, recently I found a header file which was including D3DGraphics.h when it was not using it, so I removed the
#include "D3DGraphics.h"
When I did that, the D3DGraphics header / cpp file suddenly forgot all the DirectX definitions and I got loads of errors like IDirect3D9 and D3DCOLOR_XRGB is undefined!? I have used
#pragma once
in all my header files and I'm pretty sure there is no mutual inclusion so I'm stumped. Why would removing the #include of a file cause that file to stop working!?
Thanks in advance

To anyone who had this problem: I was an idiot. Somewhere in my program I had used
#include <d3d.h>
This caused the directx version to be defined as lower than 9, so in my graphics header when I used #include <d3d9.h>
it did not define any directx9 types (facepalm.)

Try using header guards instead of #pragma once.
i.e.
#ifndef D3DGRAPHICS_H
#define D3DGRAPHICS_H
class D3DGraphics...
#endif

Related

Precompiled header problems

//........Project for ABC.dll
//ABC.h
#pragma once
class ABC{
public:
ABC(){}
private:
std::vector<int> m_vector;
};
//ABC.cpp
#include "Stdafx.h"
#include "ABC.h"
//Stdafx.h
#include <vector>
Till today, I've skipped #include <standard-lib.h> in my headers by delegating it to Stdafx.h header.
It's never been a problem when I worked in a single project file.
Now I'm trying to add a new DLL project to gather shared codes in one project.
It compiled well and generated ABC.dll too.
Here's a problem. When another project that uses ABC.dll show compile error saying that std::vector does not exist.
//...........Another Project using ABC.dll
int main(){
ABC abc;
}
Error C2039 'vector': is not a member of 'std'
To get it working, I had to include all the libraries in the consumer's Stdafx.h too.
Maybe I've been misusing the precompiled header.
I want to know whether the way I've been doing with the PCH was wrong or right.
If it's wrong, I would appreciate it if you suggest right ways of using PCH.
Thanks.
Your problems have nothing to do with precompiled headers. A good practice is to include all the stuff directly used by current file. This will prevent changes in includes of one header file from potentially requiring changes in includes in files that are using this header. vector needs to be included in ABC.h because it is directly used there. Otherwise you'll end up with endless struggling to figure out which headers needs to be included when including this particular library header.

Shortcut of Importing/Including All Library in C++

In java we can import all class from a package using '*' like - java.lang.*.
While coding in C++ we imports multiple library like this -
#include<cstdio>
#include<iostream>
.....
Is there any shortcut/way in C++ to include all these library using a single statement/line?
Thanks
No, there is no method to specify more than one file in a #include preprocessor directive.
Many people get around this dilemma by creating a monster include file that has multiple #include statements:
monster_include.h
#ifndef MONSTER_H
#define MONSTER_H
#include <iostream>
#include <string>
#endif
The disadvantage is that if any of these include files are changed, including ones not used by the source file, the source file will still be rebuilt.
I recommend creating an empty stencil header file and an empty stencil source file, then adding #include as required. The stencil can be copied then filled in as appropriate. This will save more typing time than use the megalithic include file.
You can use this library:
#include<bits/stdc++.h>
This library includes every library you need. Using this, you can delete (or comment) all the others library declarations.
See more here: How does #include bits/stdc++.h work in C++?
There's nothing available for c++ like in your java sample.
Roll your own header to include all stuff you need.
E.g.
AllProjectHeaders.h
#ifndef ALLPROJECT_HEADERS
#define ALLPROJECT_HEADERS
#include<cstdio>
#include<iostream>
// ...
#endif
You might also want to take a look at precompiled headers, it should reduce the number of includes in the source files if there is something that you include everywhere.

C++ why the #include on the bottom of header for templates?

If I have a program header file named program.h and a template named program.template, I learned that you need to #include "program.template" at the bottom of the program.h file before #endif.
This seems inconsistent with previous methods of having #include on the top of the file. Why is this?
My other question is, do you need #include "program.h" in the program.template file? Why or why not? If so, on the top or on the bottom of the file?
Thanks!
#include is a C/C++ preprocessor directive. It tells the compiler (pre processor component) to dump the contents of file X (e.g. a header file) into the source code of the current file at the #include location.
The #include directive can be used in many ways which require placing it at the start, end or the middle of another header/C/CPP file.
Without seeing your code, it's hard to tell what or why it was done.

Management of lot of #include statements and header files

In my current project. A lot of my .cpp and .h files have plenty of includes in them such as 6 or 7 of headers declared in the following manner.
#ifndef A_Header
#include "a.h"
#endif
#ifndef B_Header
#include "b.h"
#endif
I wanted to know would it make sense if I wrapped all of these headers (used in the project) in a single header and then declare that header in each source file as such
#ifndef Wrapper_Header
#include "wrapper.h" /*This would contain a collection of all headers*/
#endif
Any suggestions and drawbacks of this plan that I am not anticipating?
That's totally bizarre.
Every header should contain a header guard:
#ifndef THIS_HEADER
#define THIS_HEADER
/* contents of the header */
#endif
This goes inside the header, not in the including .cpp file. The compiler detects the header guard and avoids re-reading all the text when it's included again. This can save seconds of compilation time.
If your headers have that, then the guards in the .cpp file are extraneous and you should remove them. 6 or 7 headers isn't a lot, but that silly boilerplate does sure add up.
Never wrap your headers, always be explicit, make it clear what is included, and do not include what you do not need.
Potatoswatter is correct
But I like to add use "forward declaration".
I am sure you can google it.

how to create a header file in dev c++

I am creating header file for the fist time in dev c++
I have created add.h and add.cpp according to proper format. I don't know where to store them and when I am using header, it is showing many errors
Typically my headers look like this:
#ifndef ADD_H
#define ADD_H
class Add
{
...
};
#endif
and I save them in the same directory as my .cpp files.
In the implementation file:
#include "add.h"
And then in the main cpp file:
#include "add.h"
Doesn't matter where you save them, just put them in the same directory.
You include the header in your .cpp file like this:
#include "add.h"
Try googling for some beginner C++ tutorials.
The problem that it is showing many errors is that you may have written incorrect code. You can start a new question, paste the part of code which you think is cause of the error, with a little description about your code and we'll happily help you out :)