我正在为我的数据结构课程设计一个项目。我从来没有使用过c ++,并且我几乎没有学习如何将文件分别编译为.o文件,然后将它们链接在一起以创建可执行文件,因此要使它不给我带来错误是非常具有挑战性的。
我们得到一个:
我们应该创建一个:
此处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
这是我正在使用的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和其他版本的版本?也许这将有助于确定问题。
答案 0 :(得分:0)
您需要使用g++
而不是gcc
来编译和链接c ++代码。