Cannot Open Source File

In the Main Test File, the statement #include "frac93.hpp" does not work. There is a red squiggly line underneath the #include that indicates the file cannot be opened. That same statement is in the implementation file and there is no red squiggly line. How come it appears in the Main Test File?

Header File
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
/* 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, 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
     * - new: virtual
     */
    virtual 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
     * - new: virtual
     */
    virtual void printOn(std::ostream&) const;
    virtual void scanFrom(std::istream&);

    /* type conversion to double
     * - new: virtual
     */
    virtual double toDouble() const;

    // new: virtual destructor (without instructions)
    virtual ~Fraction() {
    }
};

/* 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
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
// inherit/frac93.cpp
#include "frac93.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);
	}

}




Main Test File
1
2
// ftest93.cpp
#include "frac93.hpp" 
Is the name of your header file FRACTION.hpp? Where is the name frac93.hpp defined?
The name of the header file is "frac93.hpp". The name "frac93.hpp" is defined in the Solution Explorer under Header Files.

I found the problem "ftest93.cpp" or the Main Test File was not in the same folder as "frac93.hpp" and "frac93.cpp". When I put "ftest93.cpp" in the sample folder the red squiggly line went away.

However, I start to write the test problem but encounter the following error messages:

1>c:\users\dexter\onedrive\object-oriented programming\inherit\frac93.hpp(11): warning C4067: unexpected tokens following preprocessor directive - expected a newline
1>c:\users\dexter\onedrive\object-oriented programming\inherit\frac93.hpp(12): error C2008: '.': unexpected in macro definition
1>frac93.cpp
1>c:\users\dexter\onedrive\object-oriented programming\inherit\frac93.hpp(11): warning C4067: unexpected tokens following preprocessor directive - expected a newline
1>c:\users\dexter\onedrive\object-oriented programming\inherit\frac93.hpp(12): error C2008: '.': unexpected in macro definition
1>Generating Code...
1>Done building project "Chapter 5.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========



The Main Test Program now looks as follows:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// ftest93.cpp
// include standard header files
#include <iostream>
#include <cstdlib>

// include header file for the classes that are being used
#include "frac93.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
}



The errors have to do with the preprocessor guard in "frac93.hpp". Any idea how to fix?
Topic archived. No new replies allowed.