Я работаю над проектом для своего класса структур данных. Я никогда не работал с c ++, и я только учусь, как по отдельности компилировать файлы в файлы .o, затем связывать их вместе, чтобы сделать исполняемый файл, поэтому очень сложно даже заставить это не выдавать ошибки мне.
Нам дано:
- заголовочный файл, stats.h
- Файл для тестирования программы, stats.cpp
- Файл, чтобы узнать, какую оценку мы получили бы, если бы представили его.
Мы должны создать:
- файл реализации, stats.cpp, который создает конструктор и несколько функций
Вот статистика.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
вот файл stats.cpp, над которым я работаю, пока я только создал конструктор, и он компилируется:
#include "stats.h"
namespace CISP430_A1
{
statistician::statistician() : count(0), total(0)
{
}
}
и это файл stattest.cpp (у меня он сохранен в HelloWorldII.cpp), который мы дали для его тестирования. Я ограничил его только той частью, которая создает три объекта статистики и выводит строку:
// 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;
}
Я скомпилировал stats.cpp и HelloWorldII.cpp примерно так:
gcc -c stats.cpp
gcc -c HelloWorldII.cpp
и затем попытался скомпилировать их в исполняемый файл следующим образом:
gcc -omyprogram stats.o HelloWorldII.cpp
что в свою очередь дает мне эту ошибку:

Что-то не так с моим конструктором? синтаксическая ошибка? Я что-то упустил в своей библиотеке?
Кроме того, что я могу ввести в командной строке, чтобы показать, какая версия OS X, gcc и другие компоненты, которые я установил? Может быть, это поможет определить проблему.