Reputation: 1259
---UPDATED ---
I have problems when including headers and cpp files in my project, so here are the files: Person.h
#ifndef PERSON_H
#define PERSON_H
class Person {
private:
string firstName;
string lastName;
long NID;
public:
Person();
void toString();
string get_firstName() {
return firstName;
}
string get_lastName() {
return lastName;
}
long get_NID() {
return NID;
}
};
#endif
Teacher which extends Person Teacher.h
#include "Person.h"
#include <iostream>
#ifndef TEACHER_H
#define TEACHER_H
class Teacher : public Person {
private:
int avg_horarium;
public:
Teacher();
void toString();
int get_avg_horarium() {
return avg_horarium;
}
};
#endif
Then here is Teacher.cpp:
#include "Teacher.h"
using namespace std;
Teacher::Teacher() : Person() {
cout << "Enter average monthly horarium: ";
cin >> avg_horarium;
}
void Teacher::toString() {
Person::toString();
cout << "Average monthly horarium: " << avg_horarium;
}
The other class which extends Person is Student and since it's similar to teacher i won't poste it here. My question is what am i doing wrong to get all these errors on the screenshot: http://s14.postimage.org/45k08ckb3/errors.jpg
Upvotes: 1
Views: 5547
Reputation: 320797
The problem is your incorrect treatment of stdafx.h
file. In MSVC compilers, when precompiled headers are enabled, everything before #include "stdafx.h"
line is ignored.
Firstly, stop including stdafx.h
into header (.h
) files. stdafx.h
is supposed to be included into implementation (.cpp
) files. In your case, #include "stdafx.h"
should be placed into Person.cpp
and Teacher.cpp
, not into Person.h
and Teacher.h
.
Secondly, either disable precompiled headers in your project, or make sure that #include "stdafx.h"
is always the very first meaningful line in each of your implementation files. All other #include
directives should go after #include "stdafx.h"
, not before.
Upvotes: 2
Reputation: 60047
Just put into the header a guard
i.e
#ifndef _THIS_FILENAME
#define _THIS_FILENAME
wibble etc
#endif
EDIT
Forgot to mention use forward declarations - saves on the expense of recompilation.
Upvotes: 0
Reputation: 48154
In your header files put;
#ifndef CLASSNAME_H
#define CLASSNAME_H
At the top of the file, after include
statements, before the class declaration. Put
#endif
At the bottom of the file after all the code. This ensures that the class is only defined once. Having multiple includes for the same header file often causes linking issues.
Upvotes: 1