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.
Related
I am getting the error "method already defined in class.obj" on ALL my methods,
I've seen that some of the solutions include separating the class into a header and a .cpp file but it's not possible in this case.
Any help will be much appreciated.
this is my h file: http://pastebin.com/k46JEQBH
the cpp has:
#include "stdafx.h"
#include "poly.h"
The problem is your definitions are in your header, and it's probably being included in multiple .cpp files. Each .cpp file is a new translation unit. Imagine you compile each .cpp file one at a time. For each .cpp file that includes your header, it will be the first time that header is encountered, POLY_H will not have been defined yet. Declarations are allowed to appear multiple times, but definitions are not. Move your definitions to a separate .cpp file and everything should work.
Edit: Keeping the definition in your header is necessary and allowed for template classes, but your class is not templated.
Could it be your usage of #pragma once? What compiler are you using?
And have you tried using include guards instead to see if that resolves the errors? For example:
#ifndef POLY_H
#define POLY_H
//your code minus the pragma once
#endif //POLY_H
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
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.
I have a very basic project in VS2012 using precompiled headers. I know that I'm supposed to add all "common" includes to stdafx.h and that I need to include this in each .cpp file. Thus, the basic setup looks like this:
Foo.h:
#ifndef FOO_H
#define FOO_H
class Foo {
public:
Foo();
~Foo();
void do(string str);
};
#endif
Foo.c:
#include "stdafx.h"
#include "Foo.h"
void Foo::do(string str) {}
in stdafx.h:
#include <string>
using namespace std;
Without precompiled headers, I'd put #include <string> into foo.h, since it has to know about the declaration of string. But how this foo.h know about string in this setup? (Note that stdafx.h is only included in the .cpp files).
Note: I have a working example that uses precompiled headers; the question is about how this works.
This is because the compiler processes headers in the order in which they appear in the main compilation unit.
Because the .cpp file included <string> (indirectly via "stdafx.h"), the contents of <string> are known to the compiler, and can be used by code that follows, even code pulled in from header files.
It is fragile though, because including your header file without first including <string> will cause errors.
You can look on the pre-compiled headers as a kind of cache for header files. The compiler analyzes a set of headers when it first encounters it (usually when compiling stdafx.cpp), compiles them, and then has the results ready for any module that needs them.
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.