How to implement files in the main.cpp file? - c++

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

Related

How to include libraries in Visual Studio automatically?

ALWAYS when I start a new class (EX: main.cpp) I need to
#include <iostream>
#include <string>
#include <math.h>
There is a way to make it automatically ? I mean every time when I create a new class they will already be included ?
A cpp file is not a class, it's a source file. A cpp file may include a class, or multiple classes, or no classes. Similar, a header filer is not a librarie, it's just a header file.
Add your includes to a header (.h file), and then your cpp file only has to include that single header to include all those common includes. Visual Studio even has something called a precompiled header, which is exactly meant as such a header with common includes, except that it's precompiled (which means, using that will compile faster than using a regular header). Afaik, you'll still have to include that single header yourself, tho, so you won't get around writing at least one #include ...
My solution is more a workaround than a "real" solution but : You need these lines at least once in your program.
So I'd create a headers_container.hpp file containing stuff my whole program needs, like these #include.
For example :
headers_container.hpp :
#include <iostream>
#include <string>
#include <math.h>
// Some stuff my whole program needs...
in your *.cpp files :
#include "headers_container.cpp"
// Your compiler knows iostream, std::strings and math now
Make sure the path to headers_container.hpp is correct (if the .hpp is not in the same folder as your .cpp
Using this method, you can add one #include in headers_container.hpp and it will update all the .cpp files.
Moreover you can write a small script to generate files (I did the script you can find here : https://gitlab.com/-/snippets/2033889 )
Enjoy your way in programming ! :)

If I need to include another header in my header and I plan to use it in my CPP file, should I include it there as well for readability?

Right so I'll try to explain my question as best as I can. Basically in one of my header files I need to use a class, obviously to do this I will have to include that class' header in the current one. I was just thinking, since I'm planning to create a new instance of this class later on down the line, should I also include it in the .cpp file to help with readability? I know that I don't have to, but would it make sense to do that? Or am I just being insane?
Example A, header file:
include "APIManager.h"
class Environment
{
public:
static void Initialize();
private:
APIManager apiManager;
};
Example A, source file:
include "Environment.h"
// Should I include "APIManager.h" here too?
void Environment::Initialize()
{
}
Tried looking around here for it, but I'm unsure of how to phrase it, so apologies if this has already been asked.
Also, in general should you be including the header files that the class's source needs in that class's header? Thanks.
If the source file is Environment.cpp, where you are implementing what you declared in Environment.h, then no, you should not reinclude it.
If on the other hand, the source file is SomeOtherFile.cpp where you are simply using the declarations from Environment.h, and you are going to use the declarations from APIManager.h separately and independently of their usage as part of Environment.h, then yes, you should. You wouldn't want a future change which removes #include <Environment.h> from SomeOtherFile.cpp to break it unexpectedly.
In other words, if SomeOtherFile.cpp has a direct dependence on APIManager.h, then that dependence should be directly expressed via a #include.
This is basically a style question, but:
No you shouldn't include it in the cpp and the header. It's overly verbose and no one does it.
I would not, except if the #include "APIManager.h" might not be in header.
<string> might be included in <iostream>, but you should include <string> anyway even if your library include include it and you use <iostream>
Include headers where they're needed.
You should include them in a header file if your class definition includes objects defined in the header you're including. If you're only using the contents of a header in the cpp, then include it in the cpp.
Never include the same external header in both file.h and file.cpp if file.cpp also includes file.h.

C++ redefining an included Class

So I have 3 classes Vehicle, Linked_List, and TrafficSim
Each class has both a .cpp and .h file
TrafficSim.h has:
#include "Linked_List.cpp"
#include "Vehicle.cpp"
Linked_List.h has:
#include "Vehicle.cpp"
and all fo the cpp files have:
#include "File.h"
All of my H files have guard that goes:
#ifndef FILENAME_H
#define FILENAME_H
/* code for class function declarations */
#endif
For some reason, after compiling Im getting an error sayng I am redefining Vehicle, so Im guessing the guard I was taught to set up, does not work. Can anyone help me?
Generally, it is the .C or .CPP files that implement classes which #include the .H header files that declare the classes, and not the other way around, as you appear to be trying to do.

difference in including header in .cpp and .h

I have a code in which I #include<linux/videodev2.h>. There are three files:
one header file- includes: stdint.h and stdlib.h. Defines a couple of functions, a struct, say abc, and some #define macros. One of the functions is
int func(int, uint32_t, size_t, abc*);
one cpp file with a lot of methods, including definition of the functions in the .h file.
one main.cpp which has main() which has a function call to the method in the .h file (complete file below). This file is only for testing purposes.
#include "head.h"
int main() {
func(5, (uint32_t)5, (size_t)5, 0);
return 0;
}
What is see is a curious case:
If I include linux/videodev2.h only in .h file, uint32_t and other things defined in this header are not accessible by the .cpp files. (erros I get are: uint32_t was not declared in this scope, and uint32_t does not name a type, among others). This happens even if the first line of the .h file is #include<linux/videodev2.h>
If I include the videodev2 header in both the cpp files, it works only if I import it (videodev2) before the .h file.
If I use func(5, (uint32_t)5, (size_t)5, (abc*)0); in the main.cpp file, I get the error that abc is not declared in this scope.
I am compiling using the command: g++ main.cpp head.cpp
I am unable to figure out why is this. I would like to include the videodev2 header in the .h file since, it is almost certain that the code using the .h file will be dependent on it. But it seems that including it in .h file has no effect at all.
I must be honest here. This was C code which I had to convert to C++. I know that I am not conforming to the best practices and standards. But why is this behaviour seen?
Remember that the #include directive indicates to the preprocessor the contents of the specified file should be treated as if they appeared directly in the source file in place of the directive (paraphrased from MSDN).
With that in mind, it seems like you are encountering improper order of #includes and also missing #includes. My guess would be that you are not including your own header file in your .cpp files. This would explain cases one and three. Consider the following files:
// header.h
// #include <linux/videodev2.h> <-- Option 1
class A {
void func(uint32_t var);
};
// header.cpp
void A::func(uint32_t var) {
// implementation
}
// main.cpp
// #include <linux/videodev2.h> <-- Option 2
#include "header.h"
// #include <linux/videodev2.h> <-- Option 3
int main() {
// implementation; something creates an instance of A and calls func
}
Now, Option 1 is not exactly desirable; it's good practice to avoid #includes in header files because they can increase build times and create unwanted dependencies. However, it will ensure that the types header.h requires are there for it to use. The essential bit is that the contents of linux/videodev2.h have to appear before the contents of header.h, anywhere that header.h is #included.
This brings me to Option 2. Option 2 will also compile correctly, because linux/videodev2.h is included before your header, and your header relies on types defined in it. Also important is that both main.cpp and header.cpp must #include "header.h", because they reference symbols declared in it.
If you were to go with Option 3, you would get compilation errors that the type uint32_t is not defined, and the compiler would point to your header file. This is because the contents of the header file appear before the contents of linux/videodev2.h, and so the compiler does not yet understand what the type uint32_t is when it encounters it.
So, given all that, you have choices: include `linux/videodev2.h' before each include of your own header file, or include it directly in your header file. I mentioned earlier that the latter is not good practice, but for your particular case, it might be the better option of the two, in case your header file needs to be included in many .cpps.
I think this would be a good opportunity to dive into precompiled headers, but I'm not as well-versed in them, so I'd leave it to someone who has more experience to explain them.
Hope this helps :)
Found the answer. There was .h.gch file in the directory. I didn't know about precompiled header. Thanks ktodisco for the insight. I still have no idea why that file was there in the first place.

source and header files

I'm facing difficulty in understanding source and header files stuff.
Suppose
1)I have a source file(functions.cpp) which contains function named 'int add(int x,int y)' in the location /Users/xyz/Desktop/functions.cpp.
2)The header file(functions.h) which contain the declaration of the functions in source file(functions.cpp) is placed in /Users/xyz/Documents/function.h
3)Other source file(main.cpp) which contain 'main()' function need to call the 'add()' function defined in 'functions.cpp'.The source file 'main.cpp' is located in /Users/xyz/Downloads/main.cpp
I'm placing these files in different locations so that i can understand these concepts better.
So,how do i link function.cpp to main.cpp using functions.h.
#include " "
What is the path that i should use in the above include?
Also,it is my understanding that .h files provides the declaration of functions which are defined some where else and having a declaration is necessary for the compiler to call the functions which are defined in some other files or functions which are not defined yet. Is that right? Please correct me in case I'm wrong.
#include "functions.h"
Your code should not know about how you choose to arrange your source tree. To hard-code paths is to earn the hatred of whoever has to maintain this code (and that includes you six months from now).
Your build system -- whatever it is -- can deal with the paths. That could be as simple as:
g++ -I/Users/xyz/Documents -c functions.cpp
Your statement of how declarations/definitions work is basically correct.
Your first question has no answer. C++ does not define how header files are found, it's up to the compiler and they all do it a bit differently. If you want an answer you'll have to look up the details in the documentation of your compiler. I would recommend you put everything the same directory and stop worrying about it.
In the second part of your question, your understanding seems pretty good to me.
You should include in your main the exact path to your header file:
#include "/Users/xyz/Documents/function.h"
Hope this help.
Regards.
You include functions.h in functions.cpp and main.cpp using #include then you compile both main.cpp and functions.cpp. The linker then links the two resulting object files. Your inclusion of functions.h in main.cpp will allow you to call the functions from functions.h within your main.cpp file
As for paths of files, provided that you specify to your compiler the required paths in which to find your code you should be fine.
You can either use a full path
#include "/Users/xyz/Documents/function.h"
or a relative path (which is usually more preferable)
#include "../Documents/function.h"
Don't forget to specify full or relative paths to your .obj files when you are linking the final executable as well ;)