1

---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

Rosen Dimov
  • 1,055
  • 12
  • 32
  • 3
    You need some form of include guard. Also, definitely don't put `using namespace std;` in your header. – chris Nov 10 '12 at 00:28
  • 1
    it's very much possible that you're up against the nemesis of every visual studio newbie: **precompiled headers**. remove the includes of "stdafx.h", and in the project properties dialog, under [Configuration Properties > C/C++ > Precompiled Headers], set "Precompiled Header" to "Not using precompiled headers". Then I suspect you'll get a whole slew of *other*, more real, errors. – Cheers and hth. - Alf Nov 10 '12 at 00:35

3 Answers3

2

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.

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
  • Okay so I disabled precompiled headers, but now it gives out meaningless errors like those: http://s18.postimage.org/5b1htuoo7/aaaaaa.png "stdafx.h" is now only included in main.cpp. – Rosen Dimov Nov 10 '12 at 01:06
  • @Rosen Dimov: Well, now you have to fix these. What is `string` supposed to mean? In standard library the it is not `string`. It is `std::string`. – AnT stands with Russia Nov 10 '12 at 01:15
  • Okay, so I tried adding a 3rd class DatabaseManager which doesn't extend any of the other classes and when I try including Teacher.h inside DatabaseManager.cpp the following error occur: http://s9.postimage.org/e2a2wt8jh/teacher.jpg My DatabaseManager.h file is here: http://s10.postimage.org/5d41zidnb/h_file.jpg – Rosen Dimov Nov 10 '12 at 12:11
1

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.

evanmcdonnal
  • 46,131
  • 16
  • 104
  • 115
  • okay i added this stuff but now it gives out this error: http://s8.postimage.org/hmhz5hcfn/sadsads.jpg – Rosen Dimov Nov 10 '12 at 00:37
  • @RosenDimov It's cause I left out the file name part. I'll add an edit. – evanmcdonnal Nov 10 '12 at 00:37
  • Sorry for the dumb question, but what value exactly should i assign to FILENAME. I tried Teacher.h or "Teacher.h" but it still produces errors. – Rosen Dimov Nov 10 '12 at 00:43
  • @RosenDimov Your question wasn't dumb, my answer was sloppy. Try `TEACHER_H` or `PERSON_H` respectively. – evanmcdonnal Nov 10 '12 at 00:44
  • 1
    @RosenDimov, Sorry if I'm misunderstanding, but the name you use requires no relation to anything else, it just has to not be used anywhere else. `FILENAME_H` is a fairly common convention. – chris Nov 10 '12 at 00:49
  • okay I updated my first post with code as it is now with all the corrections but still giving those meaningless errors: http://s18.postimage.org/5b1htuoo7/aaaaaa.png – Rosen Dimov Nov 10 '12 at 01:13
  • @RosenDimov AndreyT's answer looks pretty good to me... Wouldn't know because when I was doing this type of crap in c++ that library was never included by default. – evanmcdonnal Nov 10 '12 at 01:15
  • yeah after 100 or so attempts it compiled, his answer was absolutely accuarate, but thanks to you too guys! – Rosen Dimov Nov 10 '12 at 01:22
0

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.

Ed Heal
  • 59,252
  • 17
  • 87
  • 127
  • [Bad name choice.](http://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier) – chris Nov 10 '12 at 00:47