cannot access private member declared in class

This is a discussion on cannot access private member declared in class within the C++ Programming forums, part of the General Programming Boards category; hello all, I am one of millions who is new to C++.. I am writing this program about rational numbers ...

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    2

    cannot access private member declared in class

    hello all,
    I am one of millions who is new to C++.. I am writing this program about rational numbers and stuck here, I just cant figure where is the prob.. I already searched almost every c++ tutorial but still I am just lost... When try to execute the prog I got errors like:
    --------------------Configuration: MyRationalNumber - Win32 Debug--------------------
    Compiling...
    RationalNumbers.cpp
    rationalnumbers.cpp(43) : error C2248: 'num' : cannot access private member declared in class 'RationalNumbers'
    rationalnumbers.h(12) : see declaration of 'num'
    rationalnumbers.cpp(43) : error C2248: 'den' : cannot access private member declared in class 'RationalNumbers'
    rationalnumbers.h(13) : see declaration of 'den'
    m_RationalNumbers.cpp
    Error executing cl.exe.

    MyRationalNumber.exe - 2 error(s), 0 warning(s)

    Can you look at my code and tell me the prob?PLEASE...

    // Class declaration

    #ifndef RATIONALNUMBERS_H
    #define RATIONALNUMBERS_H
    #include<iostream>
    using namespace std;


    class RationalNumbers{

    friend ostream& operator<<(ostream &os, const RationalNumbers &f);
    private:
    int num;
    int den;

    public:
    RationalNumbers();
    RationalNumbers(int n,int d);
    RationalNumbers operator+(RationalNumbers f);
    RationalNumbers operator-(RationalNumbers f);
    int getLCD(int,int);

    };

    #endif //RATIONALNUMBERS_H

    ======================================
    Here is my class definition
    ======================================
    #include"RationalNumbers.h"
    #include<iostream>
    using namespace std;

    RationalNumbers::RationalNumbers(){
    num = 1;
    den = 1;
    }

    RationalNumbers::RationalNumbers(int n,int d){
    num = n;
    //check if denominator is not zero
    if(d)
    den = d;
    else{
    cout << "Invalid denominator for "<< this << "... Setting denominator to 1." << endl;
    den = 1;
    }
    }

    RationalNumbers RationalNumbers:perator +(RationalNumbers f){
    RationalNumbers F;
    int lcd = F.getLCD(F.den,f.den);
    F.num = lcd * num + lcd * f.num;
    F.den = lcd;
    return F;
    }

    int RationalNumbers::getLCD(int d1, int d2){
    int temp, den1,den2;
    den1 = d1;
    den2 = d2;
    //loop until value second denominator is becomes zero
    while(d2){
    temp = d2;
    d2 = d1 % d2;
    d1 = d2;
    }
    return (den1 * den2 / d1);
    }

    ostream &operator<<(ostream & os, const RationalNumbers &f){
    os << f.num << "/" << f.den;
    return os;
    }

    =================================================
    main functions
    =================================================
    #include"RationalNumbers.h"
    #include<iostream>
    using namespace std;

    int main(){
    RationalNumbers A(1,2);
    cout << A;
    cout << endl;
    return 0;
    }

    Just trying to display Rational Number A, I am not still done with the quarter of the code and I am already stuck..... 8(

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    Welcome to the boards. If you haven't already done so then please take some time to familiarise yourself with the faq:
    http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

    Make sure you have a look at the the posting guidelines:
    http://cboard.cprogramming.com/annou...ouncementid=51
    Following the rules will ensure you get a prompt answer to your question.

    Remember, too, that the board has a search facility, a link is at the top of your screen:
    http://cboard.cprogramming.com/search.php
    It will often get you a quicker answer to your questions than waiting for a response to one you have posted.


    If you have any questions about this you may ask or you can contact one of our forum leaders:

    http://cboard.cprogramming.com/showgroups.php


    Please pay special attention to the sticky post called << !! Posting Code? Read this First !! >> that tells you how to use [code][/code] tags when posting code.


    As to your problem, the code compiles fine for me. The error seems to indicate that the operator<< is not a friend, but it is in the posted code.

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    2
    sorry for not following the forum's rules... Apologies....
    Already included it in my notes...

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Now edit your original post to conform.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    You could just declare a few getter functions in RationalNumbers like
    Code:
    int get_numerator() { return num; }
    int get_denominator() { return den; }
    I prefer not to use friend functions as you have done, but maybe that's just me.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    Generally making operator<< and operator>> friends (as well as other non-member operators) is considered better than adding accessor functions just for them. The operators are still part of the interface, and its better encapsulation to make them friends than to expose the value to the world when you don't want to.

    If you can already implement the operator through the existing interface without making it a friend or exposing more internal data, then that would probably be best.

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    I guess that's true. get_numerator() and get_denominator() do seem to be reasonable public methods for a RationalNumbers class to have, though.

    And one other thing: your operator<< for RationalNumbers takes a constant reference to a RationalNumbers, and yet you try to print a pointer to RationalNumbers here:
    Code:
    cout << "Invalid denominator for "<< this << "... Setting denominator to 1." << endl;
    Maybe I'm missing something, but it doesn't seem like that will work to me.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    Hail to the king, baby. Akkernight's Avatar
    Join Date
    Oct 2008
    Location
    Faroe Islands
    Posts
    717
    And it's bad practise to use "using" in headers... Yay! I could play clever 8-)
    Currently research OpenGL

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 28
    Last Post: 07-17-2006, 12:35 AM
  2. Using private class members in static functions
    By sethjackson in forum C++ Programming
    Replies: 2
    Last Post: 09-23-2005, 10:54 AM
  3. Nested loop frustration
    By caroundw5h in forum C Programming
    Replies: 14
    Last Post: 03-15-2004, 09:45 PM
  4. can i have an example, please?
    By jlmac2001 in forum C++ Programming
    Replies: 1
    Last Post: 03-05-2003, 05:04 PM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21