how to create a header file in dev c++ - 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 :)

Related

Determine if all needed header files are included in another header file

Suppose I have a header file which should be including another header file but doesn't (for whatever reason). For example:
myHeader.h
#ifndef MYHEADER_H
#define MYHEADER_H
struct i
{
uint32_t field; // Forgot to include <cstdint>
};
#endif
This mistake can be easily hidden in the .c/.cpp files. For instance like this:
someFile.cpp
#include "myOtherHeader.h" // <cstdint> gets included through this file
#include "myHeader.h"
struct i someStruct;
someFile.cpp will compile just fine and hide the fact that I missed including cstdint in myHeader.h. This isn't a problem here, but suppose when I want to use myHeader.h in some other .cpp file, that could cause a problem.
Is there a simple way to detect this omission of the header file? There are unpleasant ways, like manually looking over the file (but that is tedious and error prone), or creating a dummy .cpp file and including just the header file in question (but that isn't scalable to a large number of header files). Is there some static analysis tool or method that would check this for me?
You do it manually.
It's kind of silly to sit through and write code to check for you and paste it in every file when you can spend a 100th of that time just heading to the top and pasting the include statement in.

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++ include header files

I'm learning C++ and following this tutorial: http://www.learncpp.com/cpp-tutorial/19-header-files/
They have named the header file that should be included to add, while I named mine 02MultipleFiles_add.cpp. So, when I get to the include part:
02MultipleFiles_add.cpp:
#ifndef ADD_H
#define ADD_H
int add( int x, int y );
#endif
02MultipleFiles.cpp:
#include "02MultipleFiles_add.h"
errors:
cannot open source file "02MultipleFiles_add.h"
identifier "add" is undefined
In the example, why is it called add.h when the file is called add.cpp?
Why can't I include my file?Thank you.
Your first file needs to be renamed from 02MultipleFiles_add.cpp to 02MultipleFiles_add.h
Header files CAN be called anything, but should, typically be called "something.h", not "something.cpp". Files called "something.cpp" are meant to be passed directly to the compiler, and not used for #include. The filename after #include should be the same as the file is called in the filesystem.
I'm pretty sure you've made a typo in the name of the file you are including, and should rename it to "02MultipleFiles_add.h" instead of "02MultipleFiles_add.cpp".
U have given .Cpp extention to your header file.
So just change the
02MultipleFiles_add.cpp
to
02MultipleFiles_add.h
and load and compile your project again.
I think it will work for sure
You should put the definition of your function in the .cpp file instead of .h file.
I think because of this it is giving error as it is trying to include is again.

How to implement files in the main.cpp file?

I have a C++ project that I am working on. I have five files that I am working with, they are as follows
1. main.cpp
2. Account.h
3. ListDA.h
4. Account.cpp
5. ListDA.cpp
I need to implement the files in the main.cpp but I am not sure how to exactly do that or which of the files listed I need to do that with. I have code written in each of the files but the code I have in the main.cpp is not doing what I am wanting it to do.
I know it's because of the lack of code I have for it to preform but I am not sure how to exactly write the code I need to preform those actions.
I am a little lost. Any advise is helpful. If you need more information to give advise please let me know I will do the best I can. Thanks
I got that issue fixed but when I run the program it is not doing what I want it to do. I thought all the code I need is in the 5 files I have for it. Any help please?
if the .cpp files are #include-ing their header file, all you need to do is to include the headerfiles in the main. Is that what you wanted to do?
main:
#include List.h
#include account.h
Account.cpp
#include account.h
Same thing for list.cpp
Assuming that your files follow the standard header (.h) and implementation (.cpp) layout, you should just be able to include your header files in your main.cpp.
For example: If I had files Class.h with the class defenition, Class.cpp with the class implementation and main.cpp where I wish to use Class, I would simply add #include "Class.h" to the top of main.cpp
#include <iostream>
#include "Class.h"
int main()
{
//...Rest of code
return 0;
}
It would be helpful to know what compiler and platform you're using if you need further instructions. (e.g. Visual Studio/gcc, Windows/Linux/Mac, etc.)
You can put class declarations in a .h or .hpp file with the same name as the class name and put the definitions in .cpp with the same name as the class name. For example in your case assuming you have an Account class, put the class declarations in Account.h file and the class definition in Account.cpp file. main.cpp file would be the application code which uses this class.
This link would give a better idea:
http://pages.cs.wisc.edu/~hasti/cs368/CppTutorial/NOTES/CLASSES-INTRO.html

#include, error LNK2005

Alirhgt, i tried to sort this one out myslef but can't. So, i have a task to build a paint program in the console, i have a set of functions dealing with the console. My task being only to connect them logically to do something useful. The problem is that everytime i #include the two files given : the .h and the .cpp file, i get the LNK2005 error that they are already defined. If i only include the header file, the functions don't do anything( i tried using one function but the console just stood there doing nothing). Can anybody tell me what i'm doing wrong? I haven't worked with C++ in a bit, so i might be doing some stupid mistake.
First off, you should never include cpp files.
Second, you might need include guards.
Format headers like this:
#ifndef FILE_H
#define FILE_H
struct foo {
int member;
};
#endif
You can read about why from here: http://en.wikipedia.org/wiki/Include_guard