C++ 无法让超载的操作员工作
我得给统计员写一份班级档案。在这种情况下,我必须重载==,!=,+,和*操作员,但我似乎无法让他们工作,我不知道我做错了什么。+运算符应该返回s1和s2中的所有数字。==确定s1==s2,并且*将s的数量乘以刻度。这是我的头文件:C++ 无法让超载的操作员工作,c++,C++,我得给统计员写一份班级档案。在这种情况下,我必须重载==,!=,+,和*操作员,但我似乎无法让他们工作,我不知道我做错了什么。+运算符应该返回s1和s2中的所有数字。==确定s1==s2,并且*将s的数量乘以刻度。这是我的头文件: // FILE: statistician.h // // CLASS PROVIDED: statistician // (a class to keep track of statistics on a sequence of real numbers)
// FILE: statistician.h
//
// CLASS PROVIDED: statistician
// (a class to keep track of statistics on a sequence of real numbers)
// This class is part of the main_statistics.
//
// CONSTRUCTOR for the statistician class:
// statistician();
// Postcondition: The object has been initialized, and is ready to accept
// a series of numbers.
//
// PUBLIC MODIFICATION member functions for the statistician class:
// void next(double nextNum)
// Postcondition: nextNum has been given to the statistician as the next number in
// the series of numbers.
// void reset();
// Postcondition: Clears statistician, ready for use.
//
// PUBLIC CONSTANT member functions for the statistician class:
// int length() const
// Postcondition: The return value is the length of the series of numbers that has
// been given to the statistician.
// double sum() const
// Postcondition: The return value is the sum of all the numbers in the series.
// double mean() const
// Precondition: length() > 0
// Postcondition: The return value is the mean of all numbers in the series.
// double getMin() const
// Precondition: length( ) > 0
// Postcondition: The return value is the smallest number in the series.
// double getMax() const
// Precondition: length( ) > 0
// Postcondition: The return value is the largest number in the series.
//
// NON-MEMBER functions for the statistician class:
// statistician operator +(const statistician& s1, const statistician& s2)
// Postcondition: The return value contains all the numbers of series s1 and series s2.
// statistician operator *(double scale, const statistician& s)
// Postcondition: The statistician that is returned contains the same
// numbers of s multiplied by the scale.
// bool operator ==(const statistician& s1, const statistician& s2)
// Postcondition: The return value is true if s1 and s2 have the same length,
// the same mean, the same minimum, the same maximum, and the same sum.
// bool operator !=(const statistician& s1, const statistician& s2)
// Postcondition: The return value is true if s1 and s2 are not identical
//
// 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 main_stats
{
class statistician
{
public:
// CONSTRUCTOR
statistician();
// MODIFICATION MEMBER FUNCTIONS
void next_number(double nextNum);
void reset();
// CONSTANT MEMBER FUNCTIONS
int length() const;
double sum() const;
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 smallest; // 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);
bool operator !=(const statistician& s1, const statistician& s2);
}
#endif
//文件:statistican.h
//
//提供的班级:统计员
//(用于跟踪实数序列的统计信息的类)
//本课程是主要统计数据的一部分。
//
//统计员类的构造函数:
//统计学家();
//后置条件:对象已初始化,可以接受
//一系列数字。
//
//统计员类的公共修改成员函数:
//下一个作废(下一个加倍)
//后置条件:下一个数字已交给统计员,作为下一个数字
//数列。
//无效重置();
//后置条件:清除统计员,准备使用。
//
//统计员类的公共常量成员函数:
//int length()常量
//Postcondition:返回值是已指定的一系列数字的长度
//已经交给统计员了。
//双和()常数
//后置条件:返回值是序列中所有数字的总和。
//双平均常数
//前提条件:长度()>0
//后置条件:返回值是序列中所有数字的平均值。
//双getMin()常量
//前提条件:长度()>0
//后置条件:返回值是序列中的最小数字。
//双getMax()常量
//前提条件:长度()>0
//后置条件:返回值是序列中的最大数字。
//
//统计员类的非成员函数:
//统计员操作员+(常量统计员和s1,常量统计员和s2)
//后置条件:返回值包含序列s1和序列s2的所有编号。
//统计员操作员*(双刻度,常数统计员和s)
//后置条件:返回的统计员包含相同的条件
//s的数量乘以刻度。
//布尔运算符==(常数统计学家和s1,常数统计学家和s2)
//后置条件:如果s1和s2具有相同的长度,则返回值为true,
//相同的平均值、相同的最小值、相同的最大值和相同的总和。
//接线员=(康斯特统计员和s1、康斯特统计员和s2)
//后置条件:如果s1和s2不相同,则返回值为true
//
//统计学家类的值语义:
//赋值和复制构造函数可用于统计学家对象。
#ifndef STATS\u H//防止重复定义
#定义统计数据
#包括
名称空间主统计信息
{
班级统计员
{
公众:
//建造师
统计学家();
//修改成员函数
作废下一个_编号(下一个加倍);
无效重置();
//常数成员函数
int length()常量;
双和()常数;
双平均常数;
双最小()常数;
双最大()常数;
//好友功能
好友统计员运算符+(常量统计员&s1,常量统计员&s2);
朋友统计员操作员*(双尺度,常数统计员和s);
私人:
int count;//序列中有多少个数字
double total;//序列中所有数字的总和
双最小;//序列中的最小数字
双最大;//序列中最大的数字
};
//统计学家类的非成员函数
布尔运算符==(常数统计学家和s1,常数统计学家和s2);
布尔运算符!=(常数统计员&s1,常数统计员&s2);
}
#恩迪夫
这是我的实现文件:
// FILE: statistician.cxx
// CLASS IMPLEMENTED: statistician (see statistician.h for documentation)
#include "stdafx.h"
#include "statistician.h"
#include <cassert>
namespace main_stats
{
statistician::statistician()
{
reset();
}
void statistician::next_number(double nextNum)
{
if (count <= 0)
{
count = 1;
total = nextNum;
smallest = nextNum;
largest = nextNum;
return;
}
else
{
count += 1;
total += nextNum;
if (nextNum < smallest)
smallest = nextNum;
if (nextNum > largest)
largest = nextNum;
}
mean();
}
void statistician::reset()
{
count = 0;
total = 0;
smallest = 0;
largest = 0;
}
int statistician::length() const
{
return count;
}
double statistician::sum() const
{
return total;
}
double statistician::mean() const
{
assert(count > 0);
return total / count;
}
double statistician::minimum() const
{
assert(count > 0);
return smallest;
}
double statistician::maximum() const
{
assert(count > 0);
return largest;
}
bool operator ==(const statistician& s1, const statistician& s2)
{
return (s1 == s2);
}
bool operator !=(const statistician& s1, const statistician& s2)
{
return !(s1 == s2);
}
statistician operator + (const statistician& s1, const statistician& s2)
{
statistician s3;
// s3 = (s1.sum + s2.sum);
return s3;
}
statistician operator * (double scale, const statistician& s)
{
double multiply;
multiply = (s.total * scale);
return s;
}
}
//文件:statistican.cxx
//实施的班级:统计员(参见统计员.h了解文件)
#包括“stdafx.h”
#包括“统计学家h”
#包括
名称空间主统计信息
{
统计学家:统计学家
{
重置();
}
无效统计员::下一个编号(双下一个)
{
如果(计算最大值)
最大=下一个;
}
均值();
}
无效统计员::重置()
{
计数=0;
总数=0;
最小=0;
最大值=0;
}
int统计学家::长度()常量
{
返回计数;
}
双重统计学家::sum()常量
{
返回总数;
}
双重统计学家::均值()常数
{
断言(计数>0);
返回总数/计数;
}
双重统计学家::最小()常数
{
断言(计数>0);
返回最小;
}
双重统计学家::最大()常数
{
断言(计数>0);
回报最大;
}
布尔运算符==(常数统计学家和s1,常数统计学家和s2)
{
返回(s1==s2);
}
接线员=(康斯特统计员和s1、康斯特统计员和s2)
{
返回!(s1==s2);
}
统计员操作员+(常量统计员和s1,常量统计员和s2)
{
统计员s3;
//s3=(s1.sum+s2.sum);
返回s3;
}
统计员操作员*(双刻度,常数统计员和s)
{
双重乘法;
乘法=(s.总*标度);
返回s;
}
}
这是我用来测试的文件:
#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <string.h> // Provides memcpy function
#include "statistician.h"
using namespace main_stats;
using namespace std;
bool close(double a, double b)
{
const double EPSILON = 1e-5;
return (fabs(a - b) < EPSILON);
}
int test1()
{
// Test program for basic statistician functions.
// Returns 62 if everything goes okay; otherwise returns 0.
statistician s, t;
int i;
double r = 0;
if (s.length() || t.length()) return 0;
if (s.sum() || t.sum()) return 0;
for (i = 1; i <= 10000; i++)
{
s.next_number(i);
r += i;
};
if (t.length() || t.sum()) return 0;
if (s.length() != 10000) return 0;
if (!close(s.sum(), r)) return 0;
if (!close(s.mean(), r / 10000)) return 0;
// Reset and then retest everything
s.reset();
t.reset();
r = 0;
if (s.length() || t.length()) return 0;
if (s.sum() || t.sum()) return 0;
for (i = 1; i <= 10000; i++)
{
s.next_number(i);
r += i;
};
if (t.length() || t.sum()) return 0;
if (s.length() != 10000) return 0;
if (!close(s.sum(), r)) return 0;
if (!close(s.mean(), r / 10000)) return 0;
return 62;
}
int test2()
{
// Test program for minimum/maximum statistician functions.
// Returns 7 if everything goes okay; otherwise returns 0.
statistician s, t, u;
double r = 1000;
char n[15] = "10000000000000";
if (s.length() || t.length()) return 0;
if (s.sum() || t.sum()) return 0;
memcpy(&r, n, sizeof(double));
r = 1 / r;
s.next_number(r);
if ((s.minimum() != r) || (s.maximum() != r)) return 0;
r *= -1;
t.next_number(r);
if ((t.minimum() != r) || (t.maximum() != r)) return 0;
u.next_number(100); u.next_number(-1); u.next_number(101); u.next_number(3);
if ((u.minimum() != -1) || (u.maximum() != 101)) return 0;
return 7;
}
int test3()
{
// Test program for + operator of the statistician
// Returns 7 if everything goes okay; otherwise returns 0.
statistician s, t, u, v;
if (s.length() || t.length()) return 0;
if (s.sum() || t.sum()) return 0;
t.next_number(5);
u.next_number(0); u.next_number(10); u.next_number(10); u.next_number(20);
v = s + s;
if (v.length() || v.sum()) return 0;
v = s + u;
if (!(u == v)) return 0;
v = t + s;
if (!(t == v)) return 0;
v = t + u;
if (v.length() != 5) return 0;
if (!close(v.sum(), 45)) return 0;
if (v.minimum() != 0) return 0;
if (v.maximum() != 20) return 0;
if (!close(v.mean(), 45.0 / 5)) return 0;
v = v + t;
if (v.length() != 6) return 0;
if (!close(v.sum(), 50)) return 0;
if (v.minimum() != 0) return 0;
if (v.maximum() != 20) return 0;
if (!close(v.mean(), 50.0 / 6)) return 0;
return 7;
}
int test4()
{
// Test program for * operator of the statistician
// Returns 7 if everything goes okay; otherwise returns 0.
statistician s, t, u;
if (s.length() || t.length()) return 0;
if (s.sum() || t.sum()) return 0;
u.next_number(0); u.next_number(10); u.next_number(10); u.next_number(20);
s = 2 * u;
if (s.length() != 4) return 0;
if (!close(s.sum(), 80)) return 0;
if (s.minimum() != 0) return 0;
if (s.maximum() != 40) return 0;
if (!close(s.mean(), 80.0 / 4)) return 0;
s = -2 * u;
if (s.length() != 4) return 0;
if (!close(s.sum(), -80)) return 0;
if (s.minimum() != -40) return 0;
if (s.maximum() != 0) return 0;
if (!close(s.mean(), -80.0 / 4)) return 0;
s = 0 * u;
if (s.length() != 4) return 0;
if (!close(s.sum(), 0)) return 0;
if (s.minimum() != 0) return 0;
if (s.maximum() != 0) return 0;
if (!close(s.mean(), 0)) return 0;
s = 10 * t;
if (s.length() != 0) return 0;
if (s.sum() != 0) return 0;
return 7;
}
int test5()
{
// Test program for == operator of the statistician.
// Returns 7 if everything goes okay; otherwise returns 0.
statistician s, t, u, v, w, x;
if (s.length() || t.length()) return 0;
if (s.sum() || t.sum()) return 0;
t.next_number(10);
u.next_number(0); u.next_number(10); u.next_number(10); u.next_number(20);
v.next_number(5); v.next_number(0); v.next_number(20); v.next_number(15);
w.next_number(0);
x.next_number(0); x.next_number(0);
if (!(s == s)) return 0;
if (s == t) return 0;
if (t == s) return 0;
if (u == t) return 0;
if (!(u == v)) return 0;
if (w == x) return 0;
return 7;
}
int main()
{
int value = 0;
int result;
cerr << "Running statistician tests:" << endl;
cerr << "TEST 1:" << endl;
cerr << "Testing next, reset, length, sum, and mean (62 points).\n";
result = test1();
value += result;
if (result > 0) cerr << "Test 1 passed." << endl << endl;
else cerr << "Test 1 failed." << endl << endl;
cerr << "\nTEST 2:" << endl;
cerr << "Testing minimum and maximum member functions (7 points).\n";
result = test2();
value += result;
if (result > 0) cerr << "Test 2 passed." << endl << endl;
else cerr << "Test 2 failed." << endl << endl;
cerr << "\nTEST 3:" << endl;
cerr << "Testing the + operator (7 points).\n";
result = test3();
value += result;
if (result > 0) cerr << "Test 3 passed." << endl << endl;
else cerr << "Test 3 failed." << endl << endl;
cerr << "\nTEST 4:" << endl;
cerr << "Testing the * operator (7 points).\n";
result = test4();
value += result;
if (result > 0) cerr << "Test 4 passed." << endl << endl;
else cerr << "Test 4 failed." << endl << endl;
cerr << "\nTEST 5:" << endl;
cerr << "Testing the == operator (7 points).\n";
result = test5();
value += result;
if (result > 0) cerr << "Test 5 passed." << endl << endl;
else cerr << "Test 5 failed." << endl << endl;
cerr << "If you submit the statistician to Dora now, this part of the\n";
cerr << "grade will be " << value << " points out of 90.\n";
system("pause");
return EXIT_SUCCESS;
}
#包括“stdafx.h”
#包括
#包括
#包括
#include//提供memcpy函数
#包括“统计学家h”
使用名称空间main_stats;
使用名称空间std;
bool关闭(双a,双b)
{
常数双ε=1e-5;
收益率(fabs(a-b)statistician operator * (double scale, const statistician& s)
{
double multiply;
multiply = (s.total * scale);
return s;
}
bool operator ==(const statistician& s1, const statistician& s2)
{
return (s1 == s2);
}
statistician operator + (const statistician& s1, const statistician& s2)
{
statistician s3; // build up new instance
s3.count = (s1.count + s2.count); // initialize it's fields based on operands
return s3; // return new instance
}
statistician operator * (double scale, const statistician& s)
{
statistician s3; // build up new instance
s3.total = (s.total * scale); // initialize it's fields based on operands
return s3; // return new instance
}
bool operator ==(const statistician& s1, const statistician& s2)
{
return (s1 == s2);
}
bool operator ==(const statistician& s1, const statistician& s2)
{
return (s1.count == s2.count); // or whatever that makes two instances of statistician "equal"
}