Implementation of Comparison Operator

I am reading Object-Oriented Programming In C++ by Nicolai M. Josuttis. In Chapter 4 of the book, he provides a header file but no source file for the header file. So, I wrote the implementation file for the header. However, the implementation file did not take the comparison operator coding. How come?


Header File (see line 48)
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
  /* The following code example is taken from the book
 * "Object-Oriented Programming in C++"
 * by Nicolai M. Josuttis, Wiley, 2002
 *
 * (C) Copyright Nicolai M. Josuttis 2002.
 * Permission to copy, use, modify, sell and distribute this software
 * is granted provided this copyright notice appears in all copies.
 * This software is provided "as is" without express or implied
 * warranty, and with no claim as to its suitability for any purpose.
 */
#ifndef FRACTION_HPP
#define FRACTION_HPP

// include standard header files
#include <iostream>

// **** BEGIN namespace CPPBook ********************************
namespace CPPBook {

class Fraction {
  protected:
    int numer;
    int denom;

  public:
    /* error class
     */
    class DenomIsZero {
    };

    /* default constructor, and one- and two-parameter constructor
     */
    Fraction(int = 0, int = 1);

    /* multiplication
     * - global friend function so that an automatic
     *     type conversion of the first operand is possible
     */
    friend Fraction operator * (const Fraction&, const Fraction&);

    // multiplicative assignment
    const Fraction& operator *= (const Fraction&);

    /* comparison
     * - global friend function so that an automatic
     *     type conversion of the first operand is possible
     */
    friend bool operator < (const Fraction&, const Fraction&);

    // output to and input from a stream
    void printOn(std::ostream&) const;
    void scanFrom(std::istream&);

    // type conversion to double
    double toDouble() const;
};

/* operator *
 * - global friend function
 * - inline defined
 */
inline Fraction operator * (const Fraction& a, const Fraction& b)
{
    /* simply multiply numerator and denominator
     * - this saves time
     */
    return Fraction(a.numer * b.numer, a.denom * b.denom);
}

/* standard output operator
 * - overloaded globally and inline defined
 */
inline
std::ostream& operator << (std::ostream& strm, const Fraction& f)
{
    f.printOn(strm);    // call member function for output
    return strm;        // return stream for chaining
}

/* standard input operator
 * - overloaded globally and inline defined
 */
inline
std::istream& operator >> (std::istream& strm, Fraction& f)
{
    f.scanFrom(strm);   // call member function for input
    return strm;        // return stream for chaining
}

} // **** END namespace CPPBook ********************************

#endif  // FRACTION_HPP



Implementation File (See lines 36 - 40)
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
// inherit/frac91.cpp
#include "frac91.hpp"

#include <cstdlib>

// ****** BEGIN namespace CPPBook ******
namespace CPPBook {

	// Constructor
	Fraction::Fraction(int n, int d)
	{
		if (d == 0){
			std::cerr << "error:  deniminator is 0" << std::endl;
			std::exit(EXIT_FAILURE);
		}
		if (d < 0){
			numer = -n;
			denom - -d;
		}
		else{
			numer = n;
			denom = d;
		}
	}

	// Multiplication Assignment
	const Fraction& Fraction::operator*= (const Fraction& f)
	{
		*this = *this * f;

		// object (first operand) is returned
		return *this;
	}

	// Comparison Operator <
	bool Fraction::operator < (const Fraction& a, const Fraction& b)
	{
		// since the numerator cannot be negative, the following is sufficient
		return a.numer * b.denom < b.numer * a.denom;
	}

	// printOn
	void Fraction::printOn(std::ostream& strm) const
	{
		strm << numer << '/' << denom;
	}

	// scanFrom
	void Fraction::scanFrom(std::istream& strm)
	{
		int n, d;
		
		// read numerator
		strm >> n;

		// read optional separator '/' and denominator
		if (strm.peek() == '/'){
			strm.get();
			strm >> d;
		}
		else{
			d = 1;
		}

		// read error?
		if (!strm) {
			return;
		}

		// denominator equals zero?
		if (d == 0){
			// set failbit
			strm.clear(strm.rdstate() | std::ios::failbit);
			return;
		}
		 
		// assign read values
		if (d < 0){
			numer = -n;
			denom = -d;
		}
		else{
			numer = n;
			denom = d;
		}
	}

	// toDouble
	double Fraction::toDouble() const
	{
		return double(numer) / double(denom);
	}

}

A friend function is not a member of the class. It is a standalone function.
Thank you.
I revised the files and included a test file. The test file, however, cannot open the header file. Why so?

Header File (I put the definition of the comparison operator towards the end)
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
/* The following code example is taken from the book
 * "Object-Oriented Programming in C++"
 * by Nicolai M. Josuttis, Wiley, 2002
 *
 * (C) Copyright Nicolai M. Josuttis 2002.
 * Permission to copy, use, modify, sell and distribute this software
 * is granted provided this copyright notice appears in all copies.
 * This software is provided "as is" without express or implied
 * warranty, and with no claim as to its suitability for any purpose.
 */
#ifndef FRACTION_HPP
#define FRACTION_HPP

// include standard header files
#include <iostream>

// **** BEGIN namespace CPPBook ********************************
namespace CPPBook {

class Fraction {
  protected:
    int numer;
    int denom;

  public:
    /* error class
     */
    class DenomIsZero {
    };

    /* default constructor, and one- and two-parameter constructor
     */
    Fraction(int = 0, int = 1);

    /* multiplication
     * - global friend function so that an automatic
     *     type conversion of the first operand is possible
     */
    friend Fraction operator * (const Fraction&, const Fraction&);

    // multiplicative assignment
    const Fraction& operator *= (const Fraction&);

    /* comparison
     * - global friend function so that an automatic
     *     type conversion of the first operand is possible
     */
    friend bool operator < (const Fraction&, const Fraction&);

    // output to and input from a stream
    void printOn(std::ostream&) const;
    void scanFrom(std::istream&);

    // type conversion to double
    double toDouble() const;
};

/* operator *
 * - global friend function
 * - inline defined
 */
inline Fraction operator * (const Fraction& a, const Fraction& b)
{
    /* simply multiply numerator and denominator
     * - this saves time
     */
    return Fraction(a.numer * b.numer, a.denom * b.denom);
}

// Comparison Operator <
inline bool operator < (const Fraction& a, const Fraction& b)
{
	// since the numerator cannot be negative, the following is sufficient
	return a.numer * b.denom < b.numer * a.denom;
}

/* standard output operator
 * - overloaded globally and inline defined
 */
inline
std::ostream& operator << (std::ostream& strm, const Fraction& f)
{
    f.printOn(strm);    // call member function for output
    return strm;        // return stream for chaining
}

/* standard input operator
 * - overloaded globally and inline defined
 */
inline
std::istream& operator >> (std::istream& strm, Fraction& f)
{
    f.scanFrom(strm);   // call member function for input
    return strm;        // return stream for chaining
}

} // **** END namespace CPPBook ********************************

#endif  // FRACTION_HPP 




Implementation File (deleted the comparison operator)
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
// inherit/frac91.cpp
#include "frac91.hpp"

#include <cstdlib>

// ****** BEGIN namespace CPPBook ******
namespace CPPBook {

	// Constructor
	Fraction::Fraction(int n, int d)
	{
		if (d == 0){
			std::cerr << "error:  deniminator is 0" << std::endl;
			std::exit(EXIT_FAILURE);
		}
		if (d < 0){
			numer = -n;
			denom = -d;
		}
		else{
			numer = n;
			denom = d;
		}
	}

	// Multiplication Assignment
	const Fraction& Fraction::operator*= (const Fraction& f)
	{
		*this = *this * f;

		// object (first operand) is returned
		return *this;
	}

	// printOn
	void Fraction::printOn(std::ostream& strm) const
	{
		strm << numer << '/' << denom;
	}

	// scanFrom
	void Fraction::scanFrom(std::istream& strm)
	{
		int n, d;
		
		// read numerator
		strm >> n;

		// read optional separator '/' and denominator
		if (strm.peek() == '/'){
			strm.get();
			strm >> d;
		}
		else{
			d = 1;
		}

		// read error?
		if (!strm) {
			return;
		}

		// denominator equals zero?
		if (d == 0){
			// set failbit
			strm.clear(strm.rdstate() | std::ios::failbit);
			return;
		}
		 
		// assign read values
		if (d < 0){
			numer = -n;
			denom = -d;
		}
		else{
			numer = n;
			denom = d;
		}
	}

	// toDouble
	double Fraction::toDouble() const
	{
		return double(numer) / double(denom);
	}




However, I added a test file from the book to check things out. It did not work. There is an error indicating that the source file "frac91.hpp" cannot be opened.
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
/* The following code example is taken from the book
 * "Object-Oriented Programming in C++"
 * by Nicolai M. Josuttis, Wiley, 2002
 *
 * (C) Copyright Nicolai M. Josuttis 2002.
 * Permission to copy, use, modify, sell and distribute this software
 * is granted provided this copyright notice appears in all copies.
 * This software is provided "as is" without express or implied
 * warranty, and with no claim as to its suitability for any purpose.
 */
// include standard header files
#include <iostream>
#include <cstdlib>

// include header file for the classes that are being used
#include "frac91.hpp"

int main()
{
    const CPPBook::Fraction a(7,3);      // declare fraction constant a
    CPPBook::Fraction x;                 // declare fraction variable x

    std::cout << a << std::endl;         // output fraction a

    // read fraction x
    std::cout << "enter fraction (numer/denom): ";
    if (! (std::cin >> x)) {
        // input error: exit program with error status
        std::cerr << "Error during input of fraction" << std::endl;
        return EXIT_FAILURE;
    }
    std::cout << "Input was: " << x << std::endl;

    // as long as x is less than 1000
    // new: instead of while (x < CPPBook::Fraction(1000))
    while (x < 1000) {
        // multiply x by a and output result
        x = x * a;
        std::cout << x << std::endl;
    }
	 system("pause");
	 return 0;
}
I retyped the test program into another file, and the source file "frac91.hpp" could be opened. I deleted the old test program, and then ran the new test program, and it worked.
Topic archived. No new replies allowed.