静态私人成员计数器

时间:2012-11-16 02:48:43

标签: c++ private-members

UPDATE ::

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

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()是私有的。挑战是我只添加私有成员而不是触摸界面例程:(朋友的功能是要避免。任何想法?

ORIGINAL POST ::

我无法在以下部分更新静态计数器变量:// MY MYBERS

规则:“不要更改,添加或删除接口的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__

更新::查看顶部

3 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

这是为了学校吗?

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

答案 2 :(得分:0)

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

static void gcdStatsCounter();