Note that there are some explanatory texts on larger screens.

plurals
  1. POStatic private member counter
    text
    copied!<p>UPDATE::</p> <p>OK so I have these new lines in the header file:</p> <pre><code>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 </code></pre> <p>Then I added into the cpp file:</p> <pre><code>int Rationalnumber::gcdStats(0);//initialization void Rationalnumber::gcdStatsCounter() { // counter incrementer gcdStats++; } void Rationalnumber::display() {// displays the gcdStats cout &lt;&lt; "gcdStats = " &lt;&lt; Rationalnumber::gcdStats &lt;&lt; endl; } void statistics() { Rationalnumber::display(); } // print statistics </code></pre> <p><strong>It is giving me the error: static void Rationalnumber::display() is private.</strong> The challenge is for me to only add private members and not touch the interface routines :( Friend functions are to be avoided. Any ideas?</p> <p>ORIGINAL POST::</p> <p>I am having trouble updating static counter variables under the section: // MY MEMBERS</p> <p>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."</p> <p>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:</p> <pre><code>void Rationalnumber::gcdStatsCounter() { gcdStats++; } </code></pre> <p>The header file:</p> <pre><code>#ifndef RATIONALNUMBER_H #define RATIONALNUMBER_H #include &lt;iostream&gt; // HEADER FILE class Rationalnumber { friend bool operator==( Rationalnumber l, Rationalnumber r ); friend bool operator!=( Rationalnumber l, Rationalnumber r ); friend bool operator&lt;( Rationalnumber l, Rationalnumber r ); friend bool operator&lt;=( Rationalnumber l, Rationalnumber r ); friend bool operator&gt;( Rationalnumber l, Rationalnumber r ); friend bool operator&gt;=( 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 &amp;operator&gt;&gt;( std::istream &amp;is, Rationalnumber &amp;r ); friend std::ostream &amp;operator&lt;&lt;( std::ostream &amp;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 &amp;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 &amp;operator=( const Rationalnumber &amp;r ); // assignment static void statistics(); // print statistics }; // Rationalnumber #endif // __RATIONALNUMBER_H__ </code></pre> <p>UPDATE::SEE TOP</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload