gpt4 book ai didi

c++ - 静态私有(private)成员(member)柜台

转载 作者:行者123 更新时间:2023-11-28 00:52:24 25 4
gpt4 key购买 nike

更新::

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

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() is private。 我面临的挑战是只添加私有(private)成员而不触及接口(interface)例程:( Friend 函数是避免。有什么想法吗?

原帖::

我在更新部分下的静态计数器变量时遇到问题://MY MEMBERS

规则:“不得更改、添加或删除接口(interface)的 GIVEN 代码成员。如果需要,您可以添加私有(private)成员,但不允许进行其他更改。”

我的问题是如何在不创建新公共(public)函数的情况下更新私有(private)成员?我知道我需要一个静态私有(private)成员函数来访问静态私有(private)成员变量。但是,每当我尝试访问静态私有(private)计数器变量时,我都会收到错误消息“未定义对...的引用”例如,gcdStats 的这个私有(private)计数器函数(声明为静态)会给我这样的错误:

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__

更新::见顶

最佳答案

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

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

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

关于c++ - 静态私有(private)成员(member)柜台,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13409648/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com