Not able to access private data members

This is a discussion on Not able to access private data members within the C++ Programming forums, part of the General Programming Boards category; Hi. Can anyone tell me why I am not being allowed to access private data members when I use this ...

  1. #1
    diligentStudent()
    Join Date
    Apr 2002
    Posts
    79

    Question Not able to access private data members

    Hi. Can anyone tell me why I am not being allowed to access private data members when I use this friend function?
    Code:
    class Card
    {
    	public:
    		Card();
    		void setSuit(string newSuit);
            	friend istream& operator >> (istream& is, Card& cd);
    	private:
    		string mSuit;
    		int mValue;
    };
    
    istream& operator >> (istream& is, Card& cd)
    {
    	is>>cd.mSuit>>cd.mValue;
    	return is;
    }
    Thanks in advance.

  2. #2
    Registered User
    Join Date
    Dec 2001
    Posts
    108
    Create public methods that return the private data...

    Code:
    class Card
    {
    	public:
    		Card();
    		void setSuit(string newSuit);
            	friend istream& operator >> (istream& is, Card& cd);
                    string getmSuit()
                    {
                             return mSuit;
                    }
                    int getmValue()
                    {
                             return mValue;
                    }
    	private:
    		string mSuit;
    		int mValue;
    };
    
    istream& operator >> (istream& is, Card& cd)
    {
    	is>>cd.getmSuit()>>cd.getmValue();
    	return is;
    }
    Last edited by DarkStar; 05-02-2004 at 07:37 PM.

  3. #3
    diligentStudent()
    Join Date
    Apr 2002
    Posts
    79
    Thanks. I did this (I tried what you said before, it didn't seem to work):
    Code:
    class Card
    {
    	public:
    		Card();
    		void setSuit(string newSuit);
                                    friend istream& operator >> (istream& is, Card& cd);
    		int getmValue();
    		string getmSuit();
    	private:
    		string mSuit;
    		int mValue;
    };
    
    istream& operator >> (istream& is, Card& cd)
    {
    	is>>cd.getmSuit()>>cd.getmValue();
    	return is;
    }
    
    int Card::getmValue()
    {
    	return mValue;
    }
    
    string Card::getmSuit()
    {
    	return mSuit;
    }
    MSVC++ 6.0 gives the following error:
    Code:
    C:\CSCI135\Assignment7\May2.cpp(54) : error C2679: binary '>>' : no operator defined which takes a right-hand operand of type 'int' (or there is no acceptable conversion)
    Thanks.

  4. #4
    diligentStudent()
    Join Date
    Apr 2002
    Posts
    79

    Question Your educated opinion

    ...also, my professor states that one should be able to access the private data members directly, since one is using the friend function, and should not have to use accessor functions. He asked which compiler I was using, and I replied "MSVC++ 6.0." Does this work on another compiler perhaps? Is he right, or must one always use accessor functions in this situation? Thanks, Steve

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,699
    Try:

    Code:
    class Card
    {
    	public:
    		Card();
    		void setSuit(string newSuit);
                                    friend istream& operator >> (istream& is, Card& cd);
    		int getmValue();
    		string getmSuit();
    friend istream& operator >> (istream& is, Card& cd)
    {
    	is>>cd.getmSuit()>>cd.getmValue();
    	return is;
    }
    
    	private:
    		string mSuit;
    		int mValue;
    };

    This won't work as expected, though, since getmsuit() & getmvalue() return copies of the data, ie: the input won't be read into the object. You could either return references from these functions, or create some public set() functions. For example:


    int & getmValue()

    - or -

    void setmValue(int)
    Code:
    bool fun(bool value)
    {
        return std::pow(std::exp(1), std::complex<float>(0, 1) 
        * std::complex<float>(std::atan(1)*(1 << (value + 2))))
        .real() > 0;
    }

  6. #6
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    Interesting. It tried your original code (first post) and it compiled in .NET but not in 6.0.
    I have always been using set and get method, but isn't friend function supposed to be able to access private member directly?
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  7. #7
    diligentStudent()
    Join Date
    Apr 2002
    Posts
    79
    ...Thanks for letting me know that you could compile it in .NET. My professor has the class using the dev_c compiler. That's a far cry from the .NET compiler isn't it?

  8. #8
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    What you posted initially should work. Dev-C++ (the GCC compiler) is very standards compliant, and from what I have heard, so is .NET. VC++6, on the other hand, is notorious for not being compliant with the standard, and that is probably your problem. I'd recommend downloading Dev-C++ to use.

  9. #9
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    Quote Originally Posted by Zach L.
    What you posted initially should work. Dev-C++ (the GCC compiler) is very standards compliant, and from what I have heard, so is .NET. VC++6, on the other hand, is notorious for not being compliant with the standard, and that is probably your problem. I'd recommend downloading Dev-C++ to use.
    Fiiuuhh, that's relieving. I thought I misunderstood the concept of friend function and had to re-learn it
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  10. #10
    diligentStudent()
    Join Date
    Apr 2002
    Posts
    79
    Thanks. I am downloading Dev-C++ now.

  11. #11
    diligentStudent()
    Join Date
    Apr 2002
    Posts
    79

    Dev-C++ Working

    Hi. I am now having a problem with this:
    Code:
    class Card
    {
      public:
        friend ifstream& operator >>(ifstream& is, Card& cd);  
        friend ostream& operator <<(ostream& os, Card cd);
      private:
        string mSuit;
        int mValue;
    };
    
    class BunchOfCards
    {
      public:
        friend ifstream& operator >>(ifstream& ifs, vector <Card>);
      private:
        vector <Card> mSomeCards;
    };
    I am getting the following error message:
    Code:
    `mSomeCards' undeclared (first use this function)
    when I try to do this:
    Code:
    ifstream& operator >>(ifstream& ifs, vector<Card>)
    {
      ifs>>mSomeCards;
      return ifs;
    }
    Any idea how I should go about this? Thanks

  12. #12
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    This
    Code:
    ifstream& operator >>(ifstream& ifs, vector<Card>)
    should be:
    Code:
    ifstream& operator >>(ifstream& ifs, BunchOfCards& bcds)
    Then, you will probably have to modify your code inside that function, since you can't just use >> to input into a vector. There's nifty ways to do it in one line using algorithms, but you'll probably just want to loop getting the value for one card at a time and pushing it back into the vector.
    Last edited by jlou; 05-03-2004 at 05:03 PM.

  13. #13
    diligentStudent()
    Join Date
    Apr 2002
    Posts
    79
    Yes, I'm getting closer, I think:
    Code:
    class Card
    {
      public:
        friend ifstream& operator >>(ifstream& is, Card& cd);  
        friend ostream& operator <<(ostream& os, Card cd);
      private:
        string mSuit;
        int mValue;
    };
    
    class BunchOfCards
    {
      public:
        friend ifstream& operator >>(ifstream& ifs, vector <Card> aDeck);
      private:
        vector <Card> mSomeCards;
    };
    And:
    Code:
    ifstream& operator >>(ifstream& ifs, vector <Card> aDeck)
    {
      ifs>>aDeck;
      return ifs;
    }
    ...but what here?
    Code:
    int main()
    {
      BunchOfCards aDeck;
      ifstream aStreamOfCards;
      aStreamOfCards.open("Deck.dat");
      while(aStreamOfCards>>aDeck)
      {
        
      system("pause");
      return 0;
    }

  14. #14
    diligentStudent()
    Join Date
    Apr 2002
    Posts
    79
    Got error here:
    Code:
    int main()
    {
      BunchOfCards aDeck;
      ifstream aStreamOfCards;
      aStreamOfCards.open("Deck.dat");
      while(aStreamOfCards>>aDeck)
      {
        mSomeCards.push_back(aDeck);
      }  
      system("pause");
      return 0;
    }
    of:
    Code:
    no match for `ifstream & >> BunchOfCards &'

  15. #15
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    You didn't make the change I suggested. Instead of accepting a vector as an argument to the operator >>, you need to take a BunchOfCards reference. Do that first, then fix the inside of the function as I mentioned before.

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  2. Question with accessing private data
    By mikahell in forum C++ Programming
    Replies: 3
    Last Post: 01-18-2008, 02:14 AM
  3. Need help with calculator program
    By Kate in forum C# Programming
    Replies: 1
    Last Post: 01-16-2004, 09:48 AM
  4. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM

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