Why am I getting this error

Write your question here.
I have a header file clockType.h
and the implementation file clockTypeImp.cpp
and the Driver Example1-4.cpp
When I compile & run the program I then get there errors:

1 0 C:\Dev-Cpp\MAlikChapter1\clockTypeImp.cpp In file included from C:\Dev-Cpp\MAlikChapter1\clockTypeImp.cpp
3 C:\Dev-Cpp\MAlikChapter1\Example1-4.cpp from C:\Dev-Cpp\MAlikChapter1\Example1-4.cpp
4 7 C:\Dev-Cpp\MAlikChapter1\clockType.h [Error] redefinition of 'class clockType'
2 0 C:\Dev-Cpp\MAlikChapter1\Example1-4.cpp In file included from C:\Dev-Cpp\MAlikChapter1\Example1-4.cpp
4 7 C:\Dev-Cpp\MAlikChapter1\clockType.h [Error] previous definition of 'class clockType'

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
  Put the code you need help with here.
here is the header:
#include <iostream>
using namespace std;

class clockType
{
public:
    void setTime(int hours, int minutes, int seconds);
		//Function to set the time
		//The time is set according to the parameters
		//Postcondition: hr = hours; min = minutes; 
	    //               sec = seconds
 		//  The function checks whether the values of hours, , 
 		//  minutes and seconds are valid. If a value is invalid, 
 		//  the default value 0 is assigned.

    void getTime(int& hours, int& minutes, int& seconds);
		//Function to return the time
		//Postcondition: hours = hr; minutes = min;
		//	           seconds = sec

    void printTime() const;
		//Function to print the time
 		//Postcondition: Time is printed in the form hh:mm:ss

    void incrementSeconds();
		//Function to increment the time by one second.
		//Postcondition: The time is incremented by one second
		//  If the before-increment time is 23:59:59, the time
 		//  is reset to 00:00:00.

    void incrementMinutes();
		//Function to increment the time by one minute.
		//Postcondition: The time is incremented by one minute 
		//  If the before-increment time is 23:59:53, the time
  		//  is reset to 00:00:53.

    void incrementHours();
		//Function to increment the time by one hour.
		//Postcondition: The time is incremented by one hour 
		//  If the before-increment time is 23:45:53, the time 
		//  is reset to 00:45:53.

	bool equalTime(const clockType& otherClock) const;
		//Function to compare the two times.
		//Postcondition: Returns true if this time is equal to
		//               otherClock; otherwise, returns false

    clockType(int hours, int minutes, int seconds);
		//Constructor with parameters 
		//The time is set according to the parameters.
		//Postconditions: hr = hours; min = minutes; 
	    //                sec = seconds
 		//  The constructor checks whether the values of hours, 
 		//  minutes, and seconds are valid. If a value is invalid, 
 		//  the default value 0 is assigned.

    clockType();
		//Default constructor with parameters 
 		//The time is set to 00:00:00.
		//Postcondition: hr = 0; min = 0; sec = 0

private:
    int hr;  //stores the hours
    int min; //stores the minutes
    int sec; //stores the seconds
};


here is the implementation:
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95

#include<iostream>
using namespace std;


clockType::clockType(int hours, int minutes, int seconds)
  {
  	  if(0 <= hours && hours < 24)
  	     hr = hours;
  	  else
		hr = 0;
		
	  if(0 <= minutes && minutes < 60)
	     min = minutes;
	  else
	     min = 0;
		 
	  if(0 <= seconds && seconds < 60)
	     sec = seconds;
	  else
	     sec = 0; 
  }
  
  clockType::clockType()           //default constructor
  {
  		hr = 0;
  		min = 0;
  		sec = 0;
  }
void clockType::setTime(int hours, int minutes, int seconds)
  {
  	  if(0 <= hours && hours < 24)
  	     hr = hours;
  	  else
		hr = 0;
		
	  if(0 <= minutes && minutes < 60)
	     min = minutes;
	  else
	     min = 0;
		 
	  if(0 <= seconds && seconds < 60)
	     sec = seconds;
	  else
	     sec = 0; 
  }
void clockType::getTime(int& hours, int& minutes, int& seconds)
{
	hours = hr;
	minutes = min;
	seconds = sec;
}
void clockType::printTime() const
{
	if(hr < 10)
		cout<< "0";
	cout<< hr << ":";
	
	if (min < 10)
		cout << "0";
	cout << min << ":";
	
	if( sec < 10)
		cout << "0";
	cout << sec;
}
void clockType::incrementHours()
{
	hr++;
	if( hr > 23)
		hr = 0;
}
void clockType::incrementMinutes()
{
	min++;
	if( min > 59)
	{
		min = 0;
	}
}
void clockType::incrementSeconds()
{
	sec++;
	if( sec > 59)
	{
		sec = 0;
		incrementMinutes()  ; // incrment the minutes
	}
}
bool clockType::equalTime(const clockType& otherClock) const
{
	return( hr == otherClock.hr
			&& min == otherClock.min
			&& sec == otherClock.sec);
}

and here is the driver:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

#include "clockType.h"
#include "clockTypeImp.cpp"

int main(){
	clockType myClock;
	clockType yourClock;
	myClock.setTime(5, 2, 30);
	myClock.printTime();
	yourClock.setTime(14, 39, 28);
	
	if(myClock.equalTime(yourClock))
	
	cout<<" Our times are the same.";
	
	else
		cout<< "Our times are differet";
}
But If I type in everything in one big file it works fine;
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include <iostream>
using namespace std;

class clockType
{
public:
    void setTime(int hours, int minutes, int seconds);
		//Function to set the time
		//The time is set according to the parameters
		//Postcondition: hr = hours; min = minutes; 
	    //               sec = seconds
 		//  The function checks whether the values of hours, , 
 		//  minutes and seconds are valid. If a value is invalid, 
 		//  the default value 0 is assigned.

    void getTime(int& hours, int& minutes, int& seconds);
		//Function to return the time
		//Postcondition: hours = hr; minutes = min;
		//	           seconds = sec

    void printTime() const;
		//Function to print the time
 		//Postcondition: Time is printed in the form hh:mm:ss

    void incrementSeconds();
		//Function to increment the time by one second.
		//Postcondition: The time is incremented by one second
		//  If the before-increment time is 23:59:59, the time
 		//  is reset to 00:00:00.

    void incrementMinutes();
		//Function to increment the time by one minute.
		//Postcondition: The time is incremented by one minute 
		//  If the before-increment time is 23:59:53, the time
  		//  is reset to 00:00:53.

    void incrementHours();
		//Function to increment the time by one hour.
		//Postcondition: The time is incremented by one hour 
		//  If the before-increment time is 23:45:53, the time 
		//  is reset to 00:45:53.

	bool equalTime(const clockType& otherClock) const;
		//Function to compare the two times.
		//Postcondition: Returns true if this time is equal to
		//               otherClock; otherwise, returns false

    clockType(int hours, int minutes, int seconds);
		//Constructor with parameters 
		//The time is set according to the parameters.
		//Postconditions: hr = hours; min = minutes; 
	    //                sec = seconds
 		//  The constructor checks whether the values of hours, 
 		//  minutes, and seconds are valid. If a value is invalid, 
 		//  the default value 0 is assigned.

    clockType();
		//Default constructor with parameters 
 		//The time is set to 00:00:00.
		//Postcondition: hr = 0; min = 0; sec = 0

private:
    int hr;  //stores the hours
    int min; //stores the minutes
    int sec; //stores the seconds
};

  void clockType::setTime(int hours, int minutes, int seconds)
  {
  	  if(0 <= hours && hours < 24)
  	     hr = hours;
  	  else
		hr = 0;
		
	  if(0 <= minutes && minutes < 60)
	     min = minutes;
	  else
	     min = 0;
		 
	  if(0 <= seconds && seconds < 60)
	     sec = seconds;
	  else
	     sec = 0; 
  }
  void clockType::getTime(int& hours, int& minutes, int& seconds)
  {
  	 hours = hr;
     minutes = min;
  	 seconds = sec;
  }
  
   void clockType::printTime() const
   {
   	    if(hr < 10)
   	       cout <<"0";
   	    cout << hr<<":";
   	    
   	    if(min < 10)
   	       cout<< "0";
   	    cout <<min<<":";
   	    
   	    if(sec < 10)
   	    	cout<<"0";
   	    cout<<sec;
   }
   void clockType::incrementSeconds()
   {
   	     sec++;
   	     if(sec >59)
   	     {
   	     	sec = 0;
   	     	incrementMinutes();   //increments the minutes
			}
   }
    void clockType::incrementMinutes()
    {
    	min++;
    	if(min > 59)
    	{
    		min = 0;
    		incrementHours();   //increments the hours
		}
}
	void clockType::incrementHours()
	{
		hr++;
		if(hr >23)
		   hr = 0;
	}
	
	bool clockType::equalTime(const clockType& otherClock) const
	{
		return(hr == otherClock.hr
		      && min == otherClock.min
			  && sec == otherClock.sec);
	}
	 clockType::clockType(int hours, int minutes, int seconds)
	 {
	 	setTime(hours, minutes, seconds);
	 }
	 
	  clockType::clockType()
	  {
	  	hr = 0;
	  	min = 0;
	  	sec = 0;
	  }
	
int main() {
	
	clockType myClock(04,54,32);
	myClock.setTime(04,54,32);
//	cout <<"The time is: " <<myClock.getTime() << endl;
	myClock.printTime();
//	myClock.incrementSeconds();
	cout <<"The time is now: "<<endl ;
//	myClock.incrementMinutes();
		cout <<"The time is now: "<<endl ;
//	myClock.incrementHours();
	cout <<"The time is now: "<<endl ;	
	myClock.printTime();
	myClock.incrementMinutes();
	myClock.incrementHours();
	
	clockType otherClock(04, 54, 32);
	cout<< endl;
	cout<<"Your clock time: ";
	   	otherClock.printTime();
	  	myClock.setTime(04,54,33);
    if(myClock.equalTime(otherClock))
	
	     cout<<endl<<"Our times are the same." ;
	
	else
		cout<<endl<<" Our times are not the same" <<endl;	
	}

Can any one please help.
It's not in the code but the error message seems to suggest that you are including clockType.h from clockTypeImp.cpp. That would mean clockType.h is being included twice, once in Example1-4.cpp and once in clockTypeImp.cpp. To prevent the content of clockType.h from being included more than once you should use include guards.

https://en.wikipedia.org/wiki/Include_guard

You should get into the habit of using include guards for all of your header files, even if they are only included once, because it doesn't hurt and it will save you a lot of headache down the road.

Note that it should never be necessary to include .cpp files. I can understand if you do it in a small program like this for simplicity but the way it's normally done is that each .cpp file is compiled separately and then everything is linked together to create an executable file. If you're using an IDE it most likely have a feature (usually called project) that helps you build programs consisting of multiple files.
Last edited on
Hello Bopaki,
Starting with the header file:

1. Replace lines 3 and 4 with this:
1
2
3
4
5
6
#ifndef _name 
#define _name

//  Code here

#endif // end !_name 

Change name to whatever you need to describe the file. Lines 3 and 4 are not needed in a header file. If lines 1 and 2 are in your header file they either need removed or they need to start with a comment (//).

Read this link about "using namespace std;" in a header file:

http://www.lonecpluspluscoder.com/2012/09/22/i-dont-want-to-see-another-using-namespace-xxx-in-a-header-file-ever-again/

There are many reasons why "using namespce std" is bad practice. do a search here and you will find many results to read on.

In the "clockTypeImp.cpp" file line 3 need to be replaced with "#include "clockType.h"". Since the "clockTypeImp.cpp" file needs to know about the class defined in "clockType.h". At first look I think everything will work, but I do wonder about the "getTime" function. That one I will have to see work.

Line 3 in main is not needed. This file should not be included in main, but part of the whole that is compiled separately and linked when the program is compiled.

I will load this up and test it and see if I can find any other problems.

Hope this helps,

Andy
Thanks Peter87
It worked like a bomb with guards
Topic archived. No new replies allowed.