UPDATE::
OK so I have these new lines in the header file:
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
Then I added into the cpp file:
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
It is giving me the error: static void Rationalnumber::display() is private. The challenge is for me to only add private members and not touch the interface routines 🙁 Friend functions are to be avoided. Any ideas?
ORIGINAL POST::
I am having trouble updating static counter variables under the section: // MY MEMBERS
The rules: “Do not change, add or remove members of the GIVEN code for the interface. You may add private members if needed, but no other changes are allowed.”
My problem is how can I update private members without creating new public functions? I understand that I need a static private member function to access a static private member variable. However, whenever I try to access the static private counter variable, I get an error “undefined reference to…” For example, this private counter function (declared as static) for gcdStats will give me such an error:
void Rationalnumber::gcdStatsCounter() {
gcdStats++;
}
The header file:
#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__
UPDATE::SEE TOP
You’ve only declared your static members, you have not defined them.
Generally with static variables, you need to declare them inside the class definition and then define them outside, for example:
Edit: Of course, the definition should go in the .cpp file, not in your header file. The reason for this is to avoid multiple definitions, which can happen if your header file is included in more than one source file. The linker is tasked with initializing static objects, and the lines below basically tell the linker that it exists and what to do with it.