Redefinition of 'struct tm' error. Header issues

I've just been copying and pasting my headers to each .h and .cpp file. Guessing this is wrong. Do i need to include guards somewhere? or which headers need to me removed?

main cpp
1
2
3
4
5
 #include <iostream>
#include <string>
#include "Club.h"
#include <ctime>

Club.h
1
2
3
 #ifndef CLUB_H
#define CLUB_H
#include "Cbros.h" 

club cpp. Sure the problem is here or in Cbros
1
2
3
4
5
6
7
8
#include "Club.h"
#include "Engineer.h"
#include "Artsy.h"
#include "Jock.h"
#include "Chics.h"
#include <iostream>
#include <string>

Cbros.h
1
2
3
4
5
#ifndef CBROS_H
#define CBROS_H

#include <iostream>
#include <string> 

Cbros.cpp
1
2
3
4
5
6
 #include "Engineer.h"
#include "Artsy.h"
#include "Jock.h"
#include "Chics.h"
#include <iostream>
#include <string> 

The other headed files are Derived classes of Cbros and only contain #include "Cbros.h"
Last edited on
Do i need to include guards somewhere?

First, could you explain what is an "include guard"?

Second, could you explain each line of Club.h? Does that file have only 3 lines?
@keskiverto

I mean like do i need to place #ifndef SOMETHING_H and #define SOMETHING_H somewhere. Also the error im getting is saying previous definition of struct tm and redefintion of tm in time.h and wchar. which arent in my project.

Club.h looks like this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#ifndef CLUB_H
#define CLUB_H

#include "Cbros.h"


class Club: public Cbros
{
    public:
        Club();
        int displayMenu();
        void processInput();
        int menuSelection;


    protected:


    private:
};

#endif // CLUB_H


 
That file has include guards. There is a preprocessor if-block that contains lines 2--21.
The block is read only if preprocessor macro "CLUB_H" has not been defined already.
Line 2 defines that macro and therefore lines 2--21 will not be read more than once.

The CLUB_H looks unique enough. In fact, if it not were unique, then some other unrelated header would not be read even once, leading to undefined symbol errors.


I do suggest that you do reorder your includes. Move the standard headers (in <>) before project's headers. That way the redefinition should occur in your files.

Please show the Cbros.h too. We want to see whether the iostream and string are both necessary for it.


Last, the compiler error messages contain line-numbers. You need to know which line in your code offends the compiler.
@keskiverto
Think the issue is in my main.cpp. I include Club.h in there so i can initialize an object to get the game started. as show below. The program runs with <ctime> if i delete Club.h out of main.cpp though.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include "Club.h"
#include <ctime>
using namespace std;

int main(){

Club Club1;
Club1.displayMenu();
Club1.processInput();




}

#endif // CLUB_H 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#ifndef CLUB_H
#define CLUB_H
#include "Cbros.h"


class Club: public Cbros
{
    public:
        Club();
        int displayMenu();
        void processInput();
        int menuSelection;


    protected:


    private:


};


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#ifndef CBROS_H
#define CBROS_H

#include <iostream>

class Cbros
{
    public:

        Cbros();
        //default constructor
        Cbros(std::string , int , double );

        std::string getName()const;
        int getAge ()const;
        int getStr()const;
        int getCharm()const;
        int getIntel()const;
        int getHeight ()const;
        void setName(std::string);
        void setAge(int);
        void setHeight(int);
        void setIntel(int);
        void setStr(int);
        void setCharm(int);
        void MeetGirl(int, int, int);
        void GirlChance(int, int, int);








       class Wallet{
    public:
        double addMoney(double amount);//Adds  Money to wallet
        double removeMoney(double amount);//removes money from wallet
        int countMoney();//displays money in wallet
    protected:
        int money;

};
    Wallet Mywallet;



    protected:

        std::string newName;
        std::string newRace;
        int newAge;
        double newHeight;
        int newStr;
        int newInt;
        int newCharm;





    private:

};



#endif // CBROS
Last edited on
Your Cbros.h should include <string> and nothing else.

As is your main.cpp requires only:
1
2
3
4
5
6
7
8
#include "Club.h"

int main() {
  Club Club1;
  Club1.displayMenu();
  Club1.processInput();
  return 0;
}

However, if you do need the <ctime>, etc, put them first:
1
2
3
4
5
6
7
8
9
10
#include <ctime>

#include "Club.h"

int main() {
  Club Club1;
  Club1.displayMenu();
  Club1.processInput();
  return 0;
}

Btw, how about <chrono>?


Last: avoid the using namespace std; -- the namespaces exist to decrease name collisions and throwing them away can only make things worse.
However, if you do need the <ctime>, etc, put them first:

Putting standard headers first masks the situation where user headers don't properly include what is needed in them. I've taken to doing things the other way 'round.

The using namespace std directives in headers is the most likely cause of the issue here. If you must use such directives, do so only in source files.
Topic archived. No new replies allowed.