Jan 28, 2016 at 11:04pm UTC
Hello everyone I recently started my 2nd semester in a desginated C++ course. We got our first homework assignment and after becoming comfortable again it went relatively smoothly. However upon getting a grade for the assignment my professor said " your implementation does not properly define the class statistician and so will not build." I'm not sure what he means, can someone help me out please.
statistician.h
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
#ifndef STATISTICIAN_H
#define STATISTICIAN_H
class statistician
{
public :
statistician();
void reset();
void next_number(double a);
int length();
double sum();
double mean();
double getMax();
double getLow();
double LastNumber();
friend statistician operator + (statistician s1, statistician s2);
friend statistician operator * (double scale, const statistician s1);
private :
double high;
double low;
int Length;
double Sum;
double recent;
double Mean;
};
#endif
statistician.cxx
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
#include <stdlib.h>
#include "statistician.h"
using namespace std;
statistician::statistician()
{
reset();
}
void statistician::next_number(double a)
{
recent = a; //stores the most previous number in the sequence
Length++; //tracks length of the sequence each time a new number is passed
if (a < low && a < high) //checks for high number
{
low = a;
}
else if (a > high && a > low) //checks for low number
{
high = a;
}
Sum += a; //adds current number to the sum
};
int statistician::length()
{
return Length;
};
double statistician::sum()
{
return Sum;
};
double statistician::mean()
{
return Sum / Length;
};
double statistician::LastNumber()
{
return recent;
};
double statistician::getMax()
{
return high;
};
double statistician::getLow()
{
return low;
};
void statistician::reset()
{
high = 0;
low = 0;
Length = 0;
Sum = 0;
Mean = 0;
recent = 0;
};
statistician operator + (statistician s1, statistician s2)
{
statistician s3;
s3.Length = s1.Length + s2.Length;
s3.Sum = s1.Sum + s2.Sum;
s3.recent = s1.LastNumber() + s2.LastNumber();
return s3;
}
statistician operator * (double num, const statistician s1)
{
statistician s2;
s2.high = num * s1.high;
s2.low = num * s1.low;
s2.Sum = num * s1.Sum;
s2.Length = s1.Length;
s2.recent = s1.recent;
return s2;
}
If anyone can point me in the right direction I would highly appreciate it!
Last edited on Jan 28, 2016 at 11:06pm UTC
Jan 28, 2016 at 11:10pm UTC
Why do your function definitions end with a semicolon? They shouldnt.
For example :
1 2 3 4 5 6 7 8 9
double statistician::sum()
{
return Sum;
}; // what is this doing here?
double statistician::mean()
{
return Sum / Length;
}; // same here
Edit:
Im pretty sure the operator overloading functions are suppose to return a reference.
1 2 3 4 5 6 7 8 9 10 11
friend statistician& operator + (statistician s1, statistician s2); // notice the &
statistician& operator + (statistician s1, statistician s2) // notice the &
{
statistician s3;
s3.Length = s1.Length + s2.Length;
s3.Sum = s1.Sum + s2.Sum;
s3.recent = s1.LastNumber() + s2.LastNumber();
return s3;
}
Edit 2: It's also almost impossible to know what you're professor means without being able to compare your assignment and your code.
your implementation does not properly define the class statistician
If your professor said this, to me it means that you did not follow his instructions correctly.
Last edited on Jan 28, 2016 at 11:15pm UTC
Jan 28, 2016 at 11:14pm UTC
Perhaps it's all the "extra" semicolons at the end of your function declarations.
For example:
main.cpp|55|error: extra ‘;’ [-Wpedantic]|
Also why are you including the C standard header <stdlib.h>, it's not need in this file and you really should include the C++ standard header <cstdlib> if you do need this include.