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.
Related
Let's say I have some .h file
random.h
#ifndef RANDOM_H
#define RANDOM_H
#include <list>
#include <vector>
#include <string>
class Random {
std::vector<std::list<std::string> > m_vectorList;
}
#endif
in the corresponding cpp file should I also
#include <list>
#include <vector>
#include <string>
so that whoever is working on the implementation doesn't need to look back to the header file, or is it bad practice to include it twice?
Basically there's no point as doing this even as a time saver because:
The programmer needs to look into the header files. Actually, header files are the first files I am looking into when discovering a new program. - Basile Starynkevitch
Not only is it bad practice, it will throw a compiler error if the header files don't have include guards (#ifndef/#define/#endif).
You are defining a new header. so you need not include pre defined header twice. It's logical. Although you can include pre defined header either in new header file or .cpp file. Also it will throw a compiler error if you defined it twice because of #ifdef condition.
#define
#ifdef
#endif
#ifndef
#define
#endif
I have recently received an assignment which defines a .h file. In this .h file at the bottom it #includes the .cpp file. For example
#ifndef CLASS_H
#define CLASS_H
class MyClass
{
//variablles and methods
};
#include MyClass.cpp
#endif
Now the problem comes in when I try to write the definitions for the .cpp file. For example when I try to define the constructor is will give me an error along the lines of
error: ‘MyClass’ does not name a type
I also incur errors when I try to define operator overloading functions
I know these errors are only applicable to the above mentioned case because when i #include my .h file in my .cpp file, it compiles perfectly
I am not allowed to change any of my .h files in this assignment
Don't do this in a header file:
#include MyClass.cpp
Compile MyClass.cpp separately and within Myclass.cpp, have:
#include MyClass.hpp
or
#include MyClass.h
Whatever you have.
I have an IOS app that is a single view application. It has a standard C++ class that I made in it, however, it is not accepting the cpp class. Here is a simplified version of the .h file(without those lines #s):
1 #ifndef __Calculator__Numbers__
2 #define __Calculator__Numbers__
3 #include <iostream>
4 class NumDigits
5 {
6 };
7 #endif
I get the error: 'iostream' file not found
It appears as if the project doesn't have the C++ libraries? If this is it, how would I add them? If not, what should I do to fix this error?
It looks like the cpp libraries are not included in my project: http://i.imgur.com/ZkJHb7j.png
Implementation files have the extension .m in Obj-C . To use a C++ file in your Xcode project with Objective-C you must use .mm extension and you can include C++ header in the .mm file. You mustn't include the header in .m file, but if you want to include your C++ header in .h, you will need a macro like:
#ifdef _CP
#include <iostream>
#endif
To solve this, I just took out all the macros, #includes, and class declarations:
#ifndef __Calculator__Numbers__
#define __Calculator__Numbers__
#include <iostream>
class NumDigits
{
};
#endif
and left only the function declarations: void myFunction(int myVariable) ;
Then in the .cpp:
#include <iostream> //and other #includes
void myFunction(int myVariable) {
//stuff
}
This worked because the functions were still called and values were passed to and from them. The return values were the values that should've been spit out. when in the .mm if the .cpp was #included, the iostream file couldn't be found so 3include the .h,and in the .cpp, #include the .h
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
When coding in either C or C++, where should I have the #include's?
callback.h:
#ifndef _CALLBACK_H_
#define _CALLBACK_H_
#include <sndfile.h>
#include "main.h"
void on_button_apply_clicked(GtkButton* button, struct user_data_s* data);
void on_button_cancel_clicked(GtkButton* button, struct user_data_s* data);
#endif
callback.c:
#include <stdlib.h>
#include <math.h>
#include "config.h"
#include "callback.h"
#include "play.h"
void on_button_apply_clicked(GtkButton* button, struct user_data_s* data) {
gint page;
page = gtk_notebook_get_current_page(GTK_NOTEBOOK(data->notebook));
...
Should all includes be in either the .h or .c / .cpp, or both like I have done here?
Put as much as you can in the .c and as little as possible in the .h. The includes in the .c are only included when that one file is compiled, but the includes for the .h have to be included by every file that uses it.
The only time you should include a header within another .h file is if you need to access a type definition in that header; for example:
#ifndef MY_HEADER_H
#define MY_HEADER_H
#include <stdio.h>
void doStuffWith(FILE *f); // need the definition of FILE from stdio.h
#endif
If header A depends on header B such as the example above, then header A should include header B directly. Do NOT try to order your includes in the .c file to satisfy dependencies (that is, including header B before header A); that is a big ol' pile of heartburn waiting to happen. I mean it. I've been in that movie several times, and it always ended with Tokyo in flames.
Yes, this can result in files being included multiple times, but if they have proper include guards set up to protect against multiple declaration/definition errors, then a few extra seconds of build time isn't worth worrying about. Trying to manage dependencies manually is a pain in the ass.
Of course, you shouldn't be including files where you don't need to.
Put as many includes in your cpp as possible and only the ones that are needed by the hpp file in the hpp. I believe this will help to speed up compilation, as hpp files will be cross-referenced less.
Also consider using forward declarations in your hpp file to further reduce the include dependency chain.
If I #include <callback.h>, I don't want to have to #include lots of other header files to get my code to compile. In callback.h you should include everything needed to compile against it. But nothing more.
Consider whether using forward declarations in your header file (such as class GtkButton;) will suffice, allowing you to reduce the number of #include directives in the header (and, in turn, my compilation time and complexity).
I propose to simply include an All.h in the project that includes all the headers needed, and every other .h file calls All.h and every .c/.cpp file only includes its own header.