classes and headers

hi, I have two classes Person and People. People is a vector of person type.

1
2
3
4
5
6
7
8
class Person{
    string name;
    int age
};

class People{
vector<Person> people;
}


now,i'm going to put them into two different header files person.h and people.h?

Should i #include "person.h" in both people.h and people.cpp??

I tried, but it still says" unknown type name Person". How can i solve this problem?
You include people.h in every file that needs to know about the people class.

i included, but it still did not work.
question: does #ifndef and #define matter?

I'm using the same names. Should i use different names?
Yes, if I understood you correctly, it does matter.

Here's a quick review on inclusion/header guards.
An inclusion/header guard (macro guard) is a construct that prevents problems that have to do with multiple inclusion of files. To be more specific, they prevent multiple definitions within the same translation unit.

Like I said, I'm not sure if I've understood you correctly, but I've tried to reconstruct the possible setup you may have, which is erroneous:

person.h
1
2
3
4
5
6
7
8
9
10
#ifndef _PERSON_H_
#define _PERSON_H_

class Person {
public :
	Person() {}
	int member = 0;
};

#endif 


people.h
1
2
3
4
5
6
7
8
9
10
11
12
13
#ifndef _PERSON_H_//wrong
#define _PERSON_H_//wrong

#include "person.h"
#include <vector>

class People {
public :
	People() {}
	std::vector<Person> peopleVec;
};

#endif 


The code I posted is incorrect, because people.h is using the same macro which person.h is using to prevent multiple inclusions. Since _PERSON_H_ will have been defined before person.h gets included, person.h will not get included, and the Person class doesn't exist in the eyes of the compiler.


Instead, give the people.h header it's own macro:

1
2
3
4
5
6
7
8
9
10
11
12
13
#ifndef _PEOPLE_H
#define _PEOPLE_H

#include "person.h"
#include <vector>

class People {
public :
	People() {}
	std::vector<Person> peopleVec;
};

#endif 
Last edited on
Let us recap. If the file has class Camera{};, then the file should be camera.h and the header guards should then be:
1
2
3
4
#ifndef _CAMERA_H
#define _CAMERA_H

#endif 


So as xismn pointed out your code would be:

person.h
1
2
3
4
5
6
7
8
#ifndef _PERSON_H
#define _PERSON_H

class Person{
      /* ... */
};

#endif 


people.h
1
2
3
4
5
6
7
8
9
10
11
#ifndef _PEOPLE_H
#define _PEOPLE_H

#include "person.h"
#include <vector>

class People{
      /* ... */
};

#endif 
i changed the #ifndef and #define, and it is working now. But there is another problem.

There are two global variables defined when i write my original .cpp. And they are used in both the people class and main.
1
2
bool verbose;
bool silent;


Where should i put these two variables? I put it in people.h, and include people.h in my man.cpp. When i compile it, it says:
1
2
3
4
5
6
7
duplicate symbol _silent in:
    people.o
    main.o
duplicate symbol _verbose in:
    people.o
    main.o
ld: 2 duplicate symbols for architecture x86_64
Last edited on
if you have a global variable declared as a duplicate somewhere else.
add the extern before it.
extern bool verbose;
extern bool silent;

this will fix the issue.
this will fix the issue.


It will complicate the issue.

Remove these as globals and create and enumerated type in your People class (for example "LogLevel") than can be either Verbose or Silent.
i put the extern there, and put
1
2
extern bool verbose;
extern bool silent;

in the people.h, it brings another error:

1
2
3
4
5
6
7
8
Undefined symbols for architecture x86_64:
  "_silent", referenced from:
      People::inputFile(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >) in people.o
      People::reset() in people.o
      ...
  "_verbose", referenced from:
      _main in main.o
ld: symbol(s) not found for architecture x86_64
Last edited on
normally with global variables i

Declare the extern in a header file:
extern bool verbose;

and define it in a single CPP file...
bool verbose = true;
Topic archived. No new replies allowed.