I'm working on a project for my data structures class. I have never worked with c++ and I'm barely learning how to individually compile files into .o files then linking those together to make the executable file so it is very challenging to even get this to not throw errors at me.
We are given a:
- header file, stats.h
- A file to test the program, stats.cpp
- A file to see what grade we'd get if we submitted it.
We are supposed to create a:
- implementation file, stats.cpp which creates a constructor and several functions
Heres stats.h:
// FILE: stats.h
// CLASS PROVIDED: statistician
// (a class to keep track of statistics on a sequence of real numbers)
// This class is part of the namespace CISP430_A1.
//
// CONSTRUCTOR for the statistician class:
// statistician( );the
// Postcondition: The object has been initialized, and is ready to accept
// a sequence of numbers. Various statistics will be calculated about the
// sequence.
//
// PUBLIC MODIFICATION member functions for the statistician class:
// void next(double r)
// The number r has been given to the statistician as the next number in
// its sequence of numbers.
// void reset( );
// Postcondition: The statistician has been cleared, as if no numbers had
// yet been given to it.
//
// PUBLIC CONSTANT member functions for the statistician class:
// int length( ) const
// Postcondition: The return value is the length of the sequence that has
// been given to the statistician (i.e., the number of times that the
// next(r) function has been activated).
// double sum( ) const
// Postcondition: The return value is the sum of all the numbers in the
// statistician's sequence.
// double mean( ) const
// Precondition: length( ) > 0
// Postcondition: The return value is the arithmetic mean (i.e., the
// average of all the numbers in the statistician's sequence).
// double minimum( ) const
// Precondition: length( ) > 0
// Postcondition: The return value is the tiniest number in the
// statistician's sequence.
// double maximum( ) const
// Precondition: length( ) > 0
// Postcondition: The return value is the largest number in the
// statistician's sequence.
//
// NON-MEMBER functions for the statistician class:
// statistician operator +(const statistician& s1, const statistician& s2)
// Postcondition: The statistician that is returned contains all the
// numbers of the sequences of s1 and s2.
// statistician operator *(double scale, const statistician& s)
// Postcondition: The statistician that is returned contains the same
// numbers that s does, but each number has been multiplied by the
// scale number.
// bool operator ==(const statistician& s1, const statistician& s2)
// Postcondition: The return value is true if s1 and s2 have the zero
// length. Also, if the length is greater than zero, then s1 and s2 must
// have the same length, the same mean, the same minimum,
// the same maximum, and the same sum.
//
// VALUE SEMANTICS for the statistician class:
// Assignments and the copy constructor may be used with statistician objects.
#ifndef STATS_H // Prevent duplicate definition
#define STATS_H
#include <iostream>
namespace CISP430_A1
{
class statistician
{
public:
// CONSTRUCTOR
statistician( );
// MODIFICATION MEMBER FUNCTIONS
void next(double r);
void reset( );
// CONSTANT MEMBER FUNCTIONS
int length( ) const { return count; }
double sum( ) const { return total; }
double mean( ) const;
double minimum( ) const;
double maximum( ) const;
// FRIEND FUNCTIONS
friend statistician operator +
(const statistician& s1, const statistician& s2);
friend statistician operator *
(double scale, const statistician& s);
private:
int count; // How many numbers in the sequence
double total; // The sum of all the numbers in the sequence
double tiniest; // The smallest number in the sequence
double largest; // The largest number in the sequence
};
// NON-MEMBER functions for the statistician class
bool operator ==(const statistician& s1, const statistician& s2);
}
#endif
here's the stats.cpp file i'm working on, so far I've only created a constructor and it compiles:
#include "stats.h"
namespace CISP430_A1
{
statistician::statistician() : count(0), total(0)
{
}
}
and this is the stattest.cpp (I have it saved under HelloWorldII.cpp) file we're given to test it out. I've restricted it to just the part that instantiates three statistician objects and prints a line out:
// FILE: stattest.cxx
// An interactive test program for the statistician class
#include <cctype> // Provides toupper
#include <iomanip> // Provides setw to set the width of an output
#include <iostream> // Provides cout, cin
#include <cstdlib> // Provides EXIT_SUCCESS
#include "stats.h"
using namespace CISP430_A1;
using namespace std;
// PROTOTYPES of functions used in this test program:
void print_menu( );
// Postcondition: A menu of choices for this program has been written to cout.
// Library facilties used: iostream.h
char get_user_command( );
// Postcondition: The user has been prompted to enter a one character command.
// A line of input (with at least one character) has been read, and the first
// character of the input line is returned.
double get_number( );
// Postcondition: The user has been prompted to enter a real number. The
// number has been read, echoed to the screen, and returned by the function.
void print_values(const statistician& s);
// Postcondition: The length, sum, minimum, mean, and maximum of s have been
// written to cout, using a field width of 10 for each of these values.
// (If length is zero, then minimum, mean, and maximum are not written.)
int main( )
{
statistician s1, s2, s3; // Three statisticians for us to play with
char choice; // A command character entered by the user
double x; // Value for multiplication x*s1
cout << "Three statisticians s1, s2, and s3 are ready to test." << endl;
}
I've compiled stats.cpp and HelloWorldII.cpp like so:
gcc -c stats.cpp
gcc -c HelloWorldII.cpp
and then tried to compile them into an executable file like this:
gcc -omyprogram stats.o HelloWorldII.cpp
which in turn gives me this error:
Is there something wrong with my constructor? a syntax error? Am I missing something in my library?
Also what can I type into the command prompt to show what version of OS X, gcc, and other stuff I have installed? Maybe that will help determine the problem.
