Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8839939
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T10:21:05+00:00 2026-06-14T10:21:05+00:00

UPDATE:: OK so I have these new lines in the header file: static void

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T10:21:06+00:00Added an answer on June 14, 2026 at 10:21 am

    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:

    class Rationalnumber
    {
    
        static float f;
        //...
    
    };
    

    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.

    //In Rationalnumber.cpp:
    float Rationalnumber::f; //Allow default initialization
    float Rationalnumber::f(5.0); //Initialize with non-default value
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have these in a jsp file. But these values are not updated in
I have a CSV file with many rows in which I need to update/replace
I have these lines of code that I use as a Factory to create
My requirement is that I want an object (tee) to update if there have
I have updated my website. Previously there were links like these: http://example.com/bla-bla-bla?language=de . After
I have a model with these 2 columns created_at: date last_updated_at: date Is it
Is there any way or possibility to update an apps code and have the
I have two panels in update panel. In panel1, there is button. If I
Is there any open source similar to Facebook's share/status update widget or I have
Possible Duplicate: Is there a way to automatically update application on Android? I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.