0

更新::

好的,所以我在头文件中有这些新行:

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__

更新::见顶部

4

3 回答 3

3

你只声明了你的静态成员,你还没有定义它们。

一般用静态变量,需要在类定义里面声明,然后在外面定义,例如:

class Rationalnumber
{

    static float f;
    //...

};

编辑:当然,定义应该放在 .cpp 文件中,而不是你的头文件中。这样做的原因是为了避免多个定义,如果您的头文件包含在多个源文件中,就会发生这种情况。链接器的任务是初始化静态对象,下面的行基本上告诉链接器它存在以及如何处理它。

//In Rationalnumber.cpp:
float Rationalnumber::f; //Allow default initialization
float Rationalnumber::f(5.0); //Initialize with non-default value
于 2012-11-16T02:58:50.577 回答
0

这是给学校的吗?

也许您的老师希望您发现朋友关键字。

于 2012-11-16T02:54:24.583 回答
0

在标题中声明您的静态方法:

静态无效 gcdStatsCounter();

于 2012-11-16T02:58:55.577 回答