extra qualification error in C++

have a member function that is defined as follows.

1
2
3
4
  const Time Time::operator+(const Time &other) { 
int hour = (*this).getHour() + other.getHour(); 
int min = (*this).getMinute() + other.getMinute(); 
int sec = (*this).getSecond()+ other.getSecond(); 


When I compile the source, I got

error: Date.cpp:16:12: error: extra qualification ‘Time::’ on member ‘operator+’ [-fpermissive] const Time Time::operator+(const Time &other) ;

What is this? How do I remove this error?

it run fine with Visual Studio
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
#include <iostream> 
#include <iomanip> 

using std::cout; 
using std::setfill; 
using std::setw; 
using std::cin; 
using std::cout; 
using std::endl; 

class Time 
{ 
public: 
Time( int = 0, int = 0 ); 

const Time Time::operator+(const Time &other) ; 
void setTime( int, int); 
void setHour( int ); 
void setMinute( int ); 

int getHour() const; 
int getMinute() const; 

void Time::get( ); 
void Time::display(); 

private: 
int hour;
int minute; 
}; 

Time::Time( int hour, int minute ) 
{ 
setTime( hour, minute); 
} 

void Time::setTime( int hour, int minute ) 
{ 
setHour( hour ); 
setMinute( minute ); 
} 

void Time::get( ) 
{ 
int hour, minute, second; 
cout << "Enter hour : " ; 
cin >> hour; 
cout << endl ;	
cout << "Enter minute : " ; 
cin >> minute; 
cout << endl ;
setTime(hour,minute); 
} 

void Time::setHour( int h ) 
{ 
hour = h; 
} 

void Time::setMinute( int m ) 
{ 
minute = m; 
} 


int Time::getHour ( ) const 
{ 
return hour; 
} 

int Time::getMinute () const 
{ 
return minute; 
} 

void Time::display() 
{ 
cout << setfill( '0' ) << setw( 2 ) << hour << ":" << setw( 2 ) 
<< minute; 
} 


const Time Time::operator+(const Time &other) { 
int hour = (*this).getHour() + other.getHour(); 
int min = (*this).getMinute() + other.getMinute(); 

if ( min > 60 ) 
{ 
min = min - 60; 
hour = hour + 1; 
} 
if ( hour > 24 ) 
{ 
hour = hour - 24; 
} 
return Time( hour, min); 
} 


int main()
{

  Time tm(12,3);
  tm.display();
  int a;
  cin >>a;
  return 0;
}


line 16 - 19
and 83 -85
Last edited on
Learn to indent.

const Time Time::operator+(const Time &other) ; in line 16 is a declaration
1
2
3
class foo{
   //declarations go here
};
as you can see, it is obvious of which class the function is a member of, so there is no need to put `Time::' in the declaration.

Also, I suppose that you actually tried to say Time operator+(const Time &other) const, that is `the function may be used with constant objects'
ok but how do u fix 83-85 line ?
if I tried to delete Date:: it will give me the error that (this*) is not compile
> if I tried to delete Date:: it will give me the error that (this*) is not compile
¿what?
http://www.cplusplus.com/forum/articles/40071/#msg216270
this is how Visual Studio say
1 IntelliSense: 'this' may only be used inside a nonstatic member function


and this is how g++ works

In file included from time.cpp:1:0:
Time.h:11:12: error: extra qualification ‘Time::’ on member ‘operator+’ [-fpermissive]
const Time Time::operator+(const Time &other) ;
^
Time.h:19:6: error: extra qualification ‘Time::’ on member ‘get’ [-fpermissive]
void Time::get( );
^
Time.h:20:6: error: extra qualification ‘Time::’ on member ‘display’ [-fpermissive]
void Time::display();
^
I told you already, remove the `Time::' in the declarations.
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
class Time 
{ 
	public: 
		Time( int = 0, int = 0 ); 

		const Time operator+(const Time &other); 
		void setTime( int, int); 
		void setHour( int ); 
		void setMinute( int ); 

		int getHour() const; 
		int getMinute() const; 

		void get();  //declaration, no Time::
		void display(); 

	private: 
		int hour;
		int minute; 
}; 

//member function definition outside of class definition
//must say which class it belongs to
void Time::get( ) 
{ 
	int hour, minute, second; 
	cout << "Enter hour : " ; 
	cin >> hour; 
	cout << endl ;	
	cout << "Enter minute : " ; 
	cin >> minute; 
	cout << endl ;
	setTime(hour,minute); 
}
Topic archived. No new replies allowed.