更新::
好的,所以我在头文件中有这些新行:
static void gcdStatsCounter();//increments counter
static void display(); // displays the gcdStats
static int gcdStats;//• calls to gcd
public: // interface routines
static void statistics(); // print statistics
然后我添加到cpp文件中:
int Rationalnumber::gcdStats(0);//initialization
void Rationalnumber::gcdStatsCounter() { // counter incrementer
gcdStats++;
}
void Rationalnumber::display() {// displays the gcdStats
cout << "gcdStats = " << Rationalnumber::gcdStats << endl;
}
void statistics() {
Rationalnumber::display();
} // print statistics
它给了我错误: static void Rationalnumber::display() 是私有的。挑战是我只添加私人成员而不接触界面例程:(要避免使用朋友功能。有什么想法吗?
原帖::
我在更新以下部分下的静态计数器变量时遇到问题://我的成员
规则:“不要更改、添加或删除接口的 GIVEN 代码的成员。如果需要,您可以添加私有成员,但不允许进行其他更改。”
我的问题是如何在不创建新公共函数的情况下更新私有成员?我知道我需要一个静态私有成员函数来访问一个静态私有成员变量。但是,每当我尝试访问静态私有计数器变量时,都会收到错误“未定义的引用...”例如,gcdStats 的这个私有计数器函数(声明为静态)会给我这样一个错误:
void Rationalnumber::gcdStatsCounter() {
gcdStats++;
}
头文件:
#ifndef RATIONALNUMBER_H
#define RATIONALNUMBER_H
#include <iostream>
// HEADER FILE
class Rationalnumber
{
friend bool operator==( Rationalnumber l, Rationalnumber r );
friend bool operator!=( Rationalnumber l, Rationalnumber r );
friend bool operator<( Rationalnumber l, Rationalnumber r );
friend bool operator<=( Rationalnumber l, Rationalnumber r );
friend bool operator>( Rationalnumber l, Rationalnumber r );
friend bool operator>=( Rationalnumber l, Rationalnumber r );
friend Rationalnumber operator+( Rationalnumber l, Rationalnumber r );
friend Rationalnumber operator-( Rationalnumber l, Rationalnumber r );
friend Rationalnumber operator*( Rationalnumber l, Rationalnumber r );
friend Rationalnumber operator/( Rationalnumber l, Rationalnumber r );
friend std::istream &operator>>( std::istream &is, Rationalnumber &r );
friend std::ostream &operator<<( std::ostream &os, Rationalnumber r );
int num, denom; // implementation
// MY MEMBERS
// functions
static int gcd(int n, int d); // for the gcd function
static void zeroDenom(); // is called whenever denom = 0
// variables
// object stats:
// The first four statistics vary depending on the implementation approach
static void gcdStatsCounter();
static int gcdStats;//• calls to gcd
static int con; //• rational-number objects created by regular constructors
static int copy; //• rational-number objects created by copy constructor
static int des; //• rational-number objects destroyed by destructor
//operation stats:
// must be consistent across implementations
static int assn; //• assignments to rational-number objects
static int rel; //• relational/equality operations between rational-number objects
static int add; //• addition/subtraction operations between rational-number objects
static int sub;
static int mul; //• multiplication/division operations between rational-number objects
static int div;
static int in; //• input/output operations on rational-number objects
static int out;
public: // interface routines
Rationalnumber();
Rationalnumber( int n );
Rationalnumber( int n, int d );
Rationalnumber( const Rationalnumber &c ); // copy constructor
~Rationalnumber();
int numerator() const; // return numerator
int numerator( int n ); // set numerator to n; return previous numerator
int denominator() const; // return denominator
int denominator( int d ); // set denominator to d; return previous denominator
Rationalnumber operator-(); // unary negation
Rationalnumber &operator=( const Rationalnumber &r ); // assignment
static void statistics(); // print statistics
}; // Rationalnumber
#endif // __RATIONALNUMBER_H__
更新::见顶部