In the following code, how do I change the line under

cout << "\nThe value of bogus in num1 is " << num1.bogus;

to get it to compile correctly. I know its calling for theValue which is private. Not sure what to change to make it work:

#include <iostream>
using namespace std;

// Class Definition for the MyInt class
class MyInt
{
    private:
       int theValue;
       
    public:
       MyInt( );
       MyInt( int );

       void setValue( int );
       int getValue ( );
       float reciprocal( );

       int bogus;     // a public data member
};

// The class implementation for the MyInt class
// Constructors
MyInt::MyInt( )
{
    theValue = 0;
    bogus = 5;
    
}

MyInt::MyInt( int n )
{
    theValue = n;
    bogus = 3;
}

// Getters and setters
void MyInt::setValue( int n )
{
    theValue = n;
}

int MyInt::getValue( )
{
    return theValue;
}

// Calculation functions
float MyInt::reciprocal( )
{
    return 1.0 / theValue;
}

// The Driver
int main ( )
{
    // Test the Constructors
    /* 1 */   MyInt num1;
    /* 2 */   MyInt num2( 2 );

    /* 3 */   cout << "\nThe value of bogus in num1 is " << num1.bogus;
            cout << "\nThe value of theValue in num1 is " << num1.theValue;

    // Test setter
    /* 4 */   num1.setValue( 5 );
    /* 5 */   cout << "\nnum1's new value = " << num1.getValue( );

    // Test reciprocal
    /* 6 */   cout << "\nnum2's reciprocal = " << num2.reciprocal( );

    cout << "\nTest complete ... hit Enter to exit.";

    system("PAUSE");
                return 0;
}

Dani AI

Generated

Short answer: theValue is a private data member, so it must be read through the class's public interface rather than accessed directly. was right to point to the public accessor; accidentally added parentheses to the private member name, which makes the compiler try to call a non-function and produces the confusing "private / cannot be used as a function" messages.

Why that happened: C++ enforces access control at compile time. Writing code that refers directly to a private member from outside the class is illegal. Putting parentheses after a name (e.g., turning a variable into something()) tells the compiler to treat it as a function call; if that name is actually a data member the compiler complains both about the access and about "not a function."

A few practical suggestions beyond the quick fix:

  • Use the public getter to read the value and the setter to change it; don’t expose implementation data as public members unless there is a strong reason.
  • Prefer constructor initializer lists and const-correct accessors. Make getValue() and reciprocal() const, and guard against division by zero in reciprocal().
  • Avoid system("PAUSE") (non-portable); use std::cin.get() or just return from main.
  • Consider overloading operator<< for clean printing instead of sprinkling cout calls.

Example of a safer, more modern small class (inline definitions for clarity):

class MyInt {
private:
    int theValue;
public:
    MyInt() : theValue(0) {}
    explicit MyInt(int n) : theValue(n) {}
    int getValue() const { return theValue; }
    void setValue(int n) { theValue = n; }
    double reciprocal() const { return theValue ? 1.0 / theValue : 0.0; }
    friend std::ostream& operator<<(std::ostream& os, const MyInt& m) {
        return os << m.theValue;
    }
};

If errors persist, rebuild the project, watch for typos (especially accidentally adding () to a data member), and confirm the member/function declarations match their definitions.

Recommended Answers

All 4 Replies

Just change it to

cout << "\nThe value of theValue in num1 is " << num1.getValue(); 
// The get and set public functions are provided to access private variables

Hmm After I added the () I get:

wksht.cpp:9: error: `int MyInt::theValue' is private
wksht.cpp:62: error: within this context
wksht.cpp:62: error: `num1.MyInt::theValue' cannot be used as a function

I didn't just append a () only. I changed what is before the () also. Look more closely.

Ahh thanks. I was thinking I had to keep theValue variable.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.