C++, Creating Math Funtions For This Weeks Program
Posted: Tue Feb 25, 2003 2:32 pm
Ok, even though I have not written the last two functions (multiplication and divition), I am trying to get the bugs worked out. I currently have seven problems trying to compile:<br><br>(43) : error C2653: 'NumDays' : is not a class or namespace name<br>(45) : error C2065: 'hours' : undeclared identifier<br>(47) : error C2065: 'days' : undeclared identifier<br>(59) : error C2653: 'NumDays' : is not a class or namespace name<br>(59) : error C2146: syntax error : missing ';' before identifier '+'<br>(59) : error C2501: 'NumDays' : missing storage-class or type specifiers<br>(59) : fatal error C1004: unexpected end of file found<br>Error executing cl.exe.<br><br><br>Here is the file I am creating:<br><br>#ifndef NUMDAYS_H<br>#define NUMDAYS_H<br><br>#include <iostream.h><br>#include <stdlib.h><br>#include <math.h><br>#include "numdays.h"<br><br>class NumDays<br>{<br>        private:<br>        double hours;<br>        double days;<br>        void calcDays ();<br><br>        public:<br>        //Constructors<br>        NumDays() {hours = 0; days = 0;}<br>        NumDays(double _hours) {hours = _hours; calcDays();}<br>        //get Function<br>        double getDays() {return days;}<br>        //Addition and Subtraction<br>        NumDays operator+(const NumDays &);<br>        NumDays operator-(const NumDays &);<br>        //Equal to and Not equal to<br>        int operator==(const NumDays &);<br>        int operator!=(const NumDays &);<br>        //Greater than and Less than<br>        int operator>(const NumDays &);<br>        int operator<(const NumDays &);<br>        //Greater than or equal to and Less than or equal to<br>        int operator>=(const NumDays &);<br>        int operator<=(const NumDays &);<br>        //Increments and Decrements<br>        NumDays operator++();<br>        NumDays operator++(double);<br>        NumDays operator--();<br>        NumDays operator--(double);<br>        //Times and Divide<br>        NumDays operator*(double);<br>        NumDays operator/(double);<br>};<br><br>#endif<br><br>/*****************************************************************/<br><br>void NumDays::calcDays()<br>{<br>        if (hours >= <!--EZCODE EMOTICON START 8) --><img src=http://www.ezboard.com/images/emoticons/glasses.gif ALT="8)"><!--EZCODE EMOTICON END--> <br>        {<br>                days += (hours / <!--EZCODE EMOTICON START 8) --><img src=http://www.ezboard.com/images/emoticons/glasses.gif ALT="8)"><!--EZCODE EMOTICON END--> ;<br>                hours = hours % 8;<br>        }<br>        else if (hours < 0)<br>        {<br>                days -= ((abs(hours) / <!--EZCODE EMOTICON START 8) --><img src=http://www.ezboard.com/images/emoticons/glasses.gif ALT="8)"><!--EZCODE EMOTICON END--> + 1);<br>                hours = 8 - (abs(hours) % <!--EZCODE EMOTICON START 8) --><img src=http://www.ezboard.com/images/emoticons/glasses.gif ALT="8)"><!--EZCODE EMOTICON END--> ;<br>        }<br>};<br><br>/*****************************************************************/<br><br>NumDays NumDays:<!--EZCODE EMOTICON START :o --><img src=http://www.ezboard.com/images/emoticons/embarassed.gif ALT=":o"><!--EZCODE EMOTICON END--> perator+(const NumDays &right)<br>{<br>        NumDays temp;<br><br>        temp.hours = hours + right.hours;<br>        temp.days = days + right.days;<br>        temp.calcDays();<br>        return temp;<br>};<br><br>/*****************************************************************/<br><br>NumDays NumDays:<!--EZCODE EMOTICON START :o --><img src=http://www.ezboard.com/images/emoticons/embarassed.gif ALT=":o"><!--EZCODE EMOTICON END--> perator-(const NumDays &right)<br>{<br>        NumDays temp;<br><br>        temp.hours = hours - right.hours;<br>        temp.days = days - right.days;<br>        temp.calcDays();<br>        return temp;<br>};<br><br>/*****************************************************************/<br><br>int NumDays:<!--EZCODE EMOTICON START :o --><img src=http://www.ezboard.com/images/emoticons/embarassed.gif ALT=":o"><!--EZCODE EMOTICON END--> perator==(const NumDays &right)<br>{<br>        if (days == right.days && hours == right.hours)<br>                return 1;<br>        else<br>                return 0;<br>};<br><br>/*****************************************************************/<br><br>int NumDays:<!--EZCODE EMOTICON START :o --><img src=http://www.ezboard.com/images/emoticons/embarassed.gif ALT=":o"><!--EZCODE EMOTICON END--> perator!=(const NumDays &right)<br>{<br>        if (days != right.days && hours != right.hours)<br>                return 1;<br>        else<br>                return 0;<br>};<br><br>/*****************************************************************/<br><br>int NumDays:<!--EZCODE EMOTICON START :o --><img src=http://www.ezboard.com/images/emoticons/embarassed.gif ALT=":o"><!--EZCODE EMOTICON END--> perator>(const NumDays &right)<br>{<br>        if (days > right.days)<br>                return 1;<br>        else if (days == right.days && hours > right.hours)<br>                return 1;<br>        else <br>                return 0;<br>};<br><br>/*****************************************************************/<br><br>int NumDays:<!--EZCODE EMOTICON START :o --><img src=http://www.ezboard.com/images/emoticons/embarassed.gif ALT=":o"><!--EZCODE EMOTICON END--> perator<(const NumDays &right)<br>{<br>        if (days < right.days)<br>                return 1;<br>        else if (days == right.days && hours < right.hours)<br>                return 1;<br>        else <br>                return 0;<br>};<br><br>/*****************************************************************/<br><br>int NumDays:<!--EZCODE EMOTICON START :o --><img src=http://www.ezboard.com/images/emoticons/embarassed.gif ALT=":o"><!--EZCODE EMOTICON END--> perator>=(const NumDays &right)<br>{<br>        if (days >= right.days)<br>                return 1;<br>        else if (days == right.days && hours > right.hours)<br>                return 1;<br>        else<br>                return 0;<br>};<br><br>/*****************************************************************/<br><br>int NumDays:<!--EZCODE EMOTICON START :o --><img src=http://www.ezboard.com/images/emoticons/embarassed.gif ALT=":o"><!--EZCODE EMOTICON END--> perator<=(const NumDays &right)<br>{<br>        if (days <= right.days)<br>                return 1;<br>        else if (days == right.days && hours < right.hours)<br>                return 1;<br>        else<br>                return 0;<br>};<br><br>/*****************************************************************/<br><br>NumDays NumDays:<!--EZCODE EMOTICON START :o --><img src=http://www.ezboard.com/images/emoticons/embarassed.gif ALT=":o"><!--EZCODE EMOTICON END--> perator++()<br>{<br>        ++hours;<br>        calcDays();<br>        return *this;<br>};<br><br>/*****************************************************************/<br><br>NumDays NumDays:<!--EZCODE EMOTICON START :o --><img src=http://www.ezboard.com/images/emoticons/embarassed.gif ALT=":o"><!--EZCODE EMOTICON END--> perator++(double)<br>{<br>        NumDays temp;<br><br>        hours++;<br>        calcDays();<br>        return temp;<br>};<br><br>/*****************************************************************/<br><br>NumDays NumDays:<!--EZCODE EMOTICON START :o --><img src=http://www.ezboard.com/images/emoticons/embarassed.gif ALT=":o"><!--EZCODE EMOTICON END--> perator--()<br>{<br>        --hours;<br>        calcDays();<br>        return *this;<br>};<br><br>/*****************************************************************/<br><br>NumDays NumDays:<!--EZCODE EMOTICON START :o --><img src=http://www.ezboard.com/images/emoticons/embarassed.gif ALT=":o"><!--EZCODE EMOTICON END--> perator--(double)<br>{<br>        NumDays temp(hours, days);<br><br>        hours--;<br>        calcDays();<br>        return temp;<br>};<br><br>/*****************************************************************/<br><br>NumDays NumDays:<!--EZCODE EMOTICON START :o --><img src=http://www.ezboard.com/images/emoticons/embarassed.gif ALT=":o"><!--EZCODE EMOTICON END--> perator*(double)<br>{<br>};<br><br>/*****************************************************************/<br><br>NumDays NumDays:<!--EZCODE EMOTICON START :o --><img src=http://www.ezboard.com/images/emoticons/embarassed.gif ALT=":o"><!--EZCODE EMOTICON END--> perator/(double)<br>{<br>};<br><br>/*****************************************************************/<br><br><br><br>The main file was given to us by the teacher:<br><br>#include <iostream.h><br>#include "numdays.h"<br><br>void main()<br>{<br>        NumDays one(25),two(15),three,four;<br>        NumDays five(12),six(1<!--EZCODE EMOTICON START 8) --><img src=http://www.ezboard.com/images/emoticons/glasses.gif ALT="8)"><!--EZCODE EMOTICON END--> ,seven(1<!--EZCODE EMOTICON START 8) --><img src=http://www.ezboard.com/images/emoticons/glasses.gif ALT="8)"><!--EZCODE EMOTICON END--> ,eight(36);<br>        NumDays nine;<br><br>        cout << "One: " << one.getDays() << endl;<br>        cout << "Two: " << two.getDays() << endl;<br>        three = one + two;<br>        cout << "Three = one + two: " << three.getDays() << endl;<br>        three = one - two;<br>        cout << "Three = one - two: " << three.getDays() << endl;<br>        four = three++;<br>        cout << "Four = Three++: " << four.getDays() << endl;<br>        four = ++three;<br>        cout << "Four = ++Three: " << four.getDays() << endl;<br>        four = three--;<br>        cout << "Four = Three--: " << four.getDays() << endl;<br>        four = --three;<br>        cout << "Four = --Three: " << four.getDays() << endl;<br>        cout << "Five: " << five.getDays() << endl;<br>        cout << "Six: " << six.getDays() << endl;<br>        cout << "Seven: " << seven.getDays() << endl;<br>        cout << "Eight: " << eight.getDays() << endl;<br>        nine = five * six;<br>        cout << "Nine = five * six: " << nine.getDays() << endl;<br>        nine = six/eight;<br>        cout << "Nine = six/eight: " << nine.getDays() << endl;<br>        if(six == seven) cout << "Six and seven are equal" << endl;<br>        else cout << "Six and seven are not equal" << endl;<br>        if(six != seven) cout << "Six and seven are not equal" << endl;<br>        else cout << "Six and seven are equal" << endl;<br>        if(seven > five) cout << "Seven is greater than five" << endl;<br>        else cout << "Seven is not greater than five" << endl;<br>        if(five > six) cout << "Five is greater than six" << endl;<br>        else cout << "Five is not greater than six" << endl;<br>        if(seven >= five) cout << "Seven is greater than or equal to five" << endl;<br>        else cout << "Seven is not greater than or equal to five" << endl;<br>        if(six >= seven) cout << "Six is greater than or equal to seven" << endl;<br>        else cout << "Six is not greater than or equal to seven" << endl;<br>        if(seven < five) cout << "Seven is less than five" << endl;<br>        else cout << "Seven is not less than five" << endl;<br>        if(five < six) cout << "Five is less than six" << endl;<br>        else cout << "Five is not less than six" << endl;<br>        if(seven <= five) cout << "Seven is less than or equal to five" << endl;<br>        else cout << "Seven is not less than or equal to five" << endl;<br>        if(six <= seven) cout << "Six is less than or equal to seven" << endl;<br>        else cout << "Six is not less than or equal to seven" << endl;<br><br>}<br><br>/*<br>Correct output:<br><br>One: 3.125<br>Two: 1.875<br>Three = one + two: 5<br>Three = one - two: 1.25<br>Four = Three++: 1.25<br>Four = ++Three: 1.5<br>Four = Three--: 1.5<br>Four = --Three: 1.25<br>Five: 1.5<br>Six: 2.25<br>Seven: 2.25<br>Eight: 4.5<br>Nine = five * six: 3.375<br>Nine = six/eight: 0.5<br>Six and seven are equal<br>Six and seven are equal<br>Seven is greater than five<br>Five is not greater than six<br>Seven is greater than or equal to five<br>Six is greater than or equal to seven<br>Seven is not less than five<br>Five is less than six<br>Seven is not less than or equal to five<br>Six is less than or equal to seven<br>Press any key to continue<br>*/ <p>-LoS-TimberWolf{FF}<br>Vehicle Specilist</p><i></i>