Question: I have this C++ script, and I know I wrote the functions correctly. My problem is with the main function. Why am I getting the
I have this C++ script, and I know I wrote the functions correctly. My problem is with the main function. Why am I getting the following error, and how do I fix it? error: member reference base type 'double' is not a structure or union
Please explain exactly why I'm getting this error, I want to learn.
/* Convert hours worked into days worked by designing a class NumDays with overloaded operators. The class must have a constructor that accepts a number of hours, as well as member functions for storing and retrieving hours & days. */
#include
using namespace std;
// Create a class NumDays with the specifications listed above: class NumDays { // Declare variables: private: double hrs; double days; public: NumDays() { // Constructor NumDays() takes the number of hours as its argument hrs = 0; days = 0; } NumDays(double h) { hrs = h; days = h/8; // because a working "day" contains 8 hours } // Define a function to retrieve the number of hours: void setHours(double h) { hrs = h; days = h/8; // because a working "day" contains 8 hours } // Define a function to store the number of hours: double getHours() const { return hrs; } // Define a function to retrieve the number of days: void setDays(double d) { days = d; hrs = d*8; // because a working "day" contains 8 hours } // Define a function to store the number of days: double getDays() const { return days; } /* Next, define the overloaded operator functions: */
// Define addition operator as X+Y NumDays operator+ (NumDays& b) { NumDays result; result.setHours(this -> getHours() + b.getHours()); return result; } // Define the subtraction operator (X-Y) NumDays operator- (NumDays& b) { NumDays result; result.setHours(this -> getHours() - b.getHours()); return result; } // Define the prefix increment operator (++X) NumDays &operator ++() { this -> setHours(this -> getHours() + 1); return *this; } // Define the postfix increment operator (X++) const NumDays operator ++(int) { NumDays result = *this; this -> setHours(this -> getHours() + 1); return result; } // Define the prefix decrement operator (--X) NumDays &operator --() { this -> setHours(this -> getHours() - 1); return *this; } // Define the postfix increment operator (X--) const NumDays operator --(int) { NumDays result = *this; this -> setHours(this -> getHours() - 1); return result; } };
/* Main Function */ int main() { // Initialize a variable num & another variable X: double num, X; // Obtain num from stdinput cout << "Please enter the number of hours worked:" << endl; cin >> num; // Call the getDays function to convert & store the hours to days into the variable X: X.getDays(); cout << num << " hours worked equals " << X << " days worked." << endl; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
