Thread: Why is thisís friend function not accessing private variables???

  1. #1
    Registered User
    Join Date
    Mar 2019
    Posts
    50

    Why is thisís friend function not accessing private variables???

    Code:
    #include <iostream>
    using namespace std;
    
    
    class StankFist
    {
        public:
            StankFist(){stinkyVar=0;}
        private:
              int stinkyVar;
        friend void stinkysFriend(StankFist &sfo);
    };
    
    
    void stinkyFriend(StankFist &sfo)
    {
        sfo.stinkyVar=99;
        cout << sfo.stinkyVar<< endl;
    }
    
    
    int main()
    {
        StankFist bob;
        stinkysFriend(bob);
        return 0;
    }
    Why am i getting this error when the function is a friend?

    error: ‘stinkyVar” is a private member of ‘StankFist’
    sfo.stinkyVar=99;
    - —^

    note: declared private here
    int stinkyVar;
    - - ^
    Last edited by Ctylersills; 05-19-2019 at 12:42 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,064
    What happens when you rename stinkyFriend to stinkysFriend?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Mar 2019
    Posts
    50
    Quote Originally Posted by laserlight View Post
    What happens when you rename stinkyFriend to stinkysFriend?
    fair enough, you got me. BUT, since I switched to C++ the compiler used ALMOST always catches my typos so I thought It was right and didn’t pay attention. I got way to dependent WAY to fast to error messages that end in “did you mean?” lol Thanks LL
    Last edited by Ctylersills; 05-19-2019 at 01:15 AM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,064
    Unfortunately, the issue here is that your mistake would normally have triggered a link error, but because access to a private member variable was involved, it triggered a compile error first, and so you never did get to see the link error, but instead were presented with an error that had to do with accessing a private member variable.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Mar 2019
    Posts
    50
    Quote Originally Posted by laserlight View Post
    Unfortunately, the issue here is that your mistake would normally have triggered a link error, but because access to a private member variable was involved, it triggered a compile error first, and so you never did get to see the link error, but instead were presented with an error that had to do with accessing a private member variable.
    AHHH, that makes total sense, I love getting an inside view of how the compiler works. Anyways, like I said I have to stop being so dependent on the ďYou spelled it wrong dummyĒ errors though, and start using search and find to make sure iím not misspelling anything before I post.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Accessing private QString with public function via inheritance
    By ubmattpangolin in forum C++ Programming
    Replies: 4
    Last Post: 05-06-2016, 10:29 PM
  2. Accessing Private Variables in Structures
    By hotshotennis in forum C++ Programming
    Replies: 4
    Last Post: 04-19-2013, 02:34 AM
  3. Nested Class Accessing Classes Private Variables
    By SterlingM in forum C++ Programming
    Replies: 1
    Last Post: 03-07-2011, 01:06 AM
  4. Replies: 12
    Last Post: 01-09-2007, 04:26 PM
  5. Setting And Accessing Private Member Variables
    By bumfluff in forum C++ Programming
    Replies: 4
    Last Post: 04-12-2006, 06:00 PM

Tags for this Thread