0

First off, this is for academic purposes. I'm extremely frustrated, and I have even been working this out with a friend for half the day.

We're working with both overloaded operators and friend classes and this lab has made me feel like a complete idiot. I'll post the important code below but the whole project will be in a zip file at the bottom of the post. I'm sure it's full of errors and it's now refusing to save some of my code (it keeps throwing an exception).

This is the header file code for my primary class

class NumDays
{
private:
    int hours;
    double days;

    void calcDays(int);
public:
    NumDays (int);
    void setHours (int);
    double getWork();
    NumDays operator+ (const NumDays &);
    NumDays operator- (const NumDays &);
    NumDays operator++ ();
    void operator<< (const NumDays &);

    friend class Overtime(const NumDays &);
};

It doesn't seem to like my attempt to pass in a NumDays object into the Overtime class constructor, but from my understanding, this is how it should work because the overtime objects are constructed with each NumDays object

The second issue is with the overloaded operators, I'm having less trouble with wrapping my head around this and more trouble with why it has to be implemented so specifically.

This is the code for the overloaded operators

/*overloading the + operator***************************************************/

NumDays NumDays::operator+ (const NumDays &right)
{
    NumDays temp;

    temp.hours = hours + right.hours;
    return temp;
}

/*overloading the -operator***************************************************/

NumDays NumDays::operator- (const NumDays &right)
{
    NumDays temp;

    temp.hours = hours - right.hours;
    return temp;
}

/*overloading the ++ operator**************************************************/

NumDays NumDays::operator++ ()
{
    ++hours;
    return *this;
}

/*overloading the << operator**************************************************/

void NumDays::operator<< (const NumDays &objOput)
{
    cout << objOput.getWork << " days have been worked by this work";
}

It's definitely not a fan of the << overloading, but I basically copied code directly from my book to try and do this.

Original code link was broken, here is a permanent link to it. http://www.mediafire.com/file/j4q3fln9a8p98ll/dayCounter.zip

Also somewhat fixed my code. Doesn't work perfectly, but it does compile and mostly work. http://www.mediafire.com/file/g5m21drbuab8tso/Lab5workCounter.zip

  • `cout << objOput.getWork << " days have been worked by this work";` -- `objOput.getWork` is a function. See anything wrong with that line, like missing parentheses? – PaulMcKenzie Oct 21 '17 at 03:06
  • 1
    That's not how a `friend` declaration works (neither syntactically nor wrt. semantics), and you don't need one. – Cheers and hth. - Alf Oct 21 '17 at 03:06
  • Agree with @Cheersandhth.-Alf. Have another look at [`friend` declarations](http://en.cppreference.com/w/cpp/language/friend). – R Sahu Oct 21 '17 at 03:08
  • I wouldn't be using a friend class if it wasn't required :/ I appreciate the help. – Trace Langfels Oct 21 '17 at 03:12
  • Also, my friend and I have gotten it compile and are busy going through and testing each function as we go along. It's tedious, but we have to be thorough. – Trace Langfels Oct 21 '17 at 03:36
  • Prefix `operator++` needs to return a reference. See [What are the basic rules and idioms for operator overloading](https://stackoverflow.com/questions/4421706/) – Remy Lebeau Oct 21 '17 at 03:47
  • I pretty much copied that code directly from the book, where it returns a dereferenced *this pointer. It's extremely confusing. – Trace Langfels Oct 21 '17 at 04:11
  • Ok, so after doing a bit of rewriting, the only major error I get is an error with the overloaded << operator. `[Error] passing 'const NumDays' as 'this' argument of 'double NumDays::getWork()' discards qualifiers [-fpermissive]` – Trace Langfels Oct 21 '17 at 04:32
  • It now looks like this codewise: `ostream &operator<< (ostream &cout, const NumDays &objOput) { cout << objOput.getWork() << " days have been worked by this worker.\n"; }` – Trace Langfels Oct 21 '17 at 04:37

1 Answers1

0
  1. You seem to have combined the syntax for a friend class and a friend constructor. (It’s not clear from your included code whether you need either.)
  2. NumDays has no default constructor, so you can’t make one and then set its hours member in the operators.
  3. Return the type NumDays& from operator++(). (This is “merely” a good idea; it doesn’t require any other code changes here.)
  4. Don’t define an output operator<< as a class member, since it’s supposed to take a std::ostream& first argument.
  5. From PaulMcKenzie’s comment: put () on your method calls.
  6. From your comment: never put a using directive in a header file (except inside a function or a detail namespace).
Davis Herring
  • 36,443
  • 4
  • 48
  • 76
  • Hey, I really appreciate it. I did do some corrections, but I still had issues. I ended up having to submit it "as is" but here is the state that I was able to submit it in. I had some odd, but consistent behavior. Compile it and see what I mean. https://1drv.ms/u/s!AtYA5536lndTgaVWVBF-Nq3Ktr0RSA – Trace Langfels Oct 21 '17 at 05:01