Thread: Access Inner's class member

  1. #1
    Registered User ch4's Avatar
    Join Date
    Jan 2007
    Posts
    152

    Access Inner's class member

    Hello fellows,

    I want to access protected member of inner class from within friend function of the outer class, without adding getter to inner class. Is that possible ? Any advice ?

    Code:
    #include <iostream>
    #include <string>
    #include <cmath>
    
    
    class Polynomial {
    protected:
    	class Term {
    		protected:
    			int exponent;
    			int coefficient;
    			Term *next;
    			Term(int exp, int coeff, Term *n)
    			{
    				exponent = exp;
    				coefficient = coeff;
    				next = n;
    			}
    			friend class Polynomial;
    
    	};
    
     private:
    	 Term * start;
    
     public:
    	 Polynomial() { start = NULL; }
    
               // ...
    
              friend ostream & operator << (ostream &out, const Polynomial &p);
    };
    
    ostream & operator << (ostream &out, const Polynomial &p)
    {
    	Polynomial::Term *temp = p.start;
    
    	while (temp != NULL)
    	{
    		if (temp->coefficient < 0)
    			out << "- ";
    		else
    			out << "+ ";
    
    		if (temp->coefficient != 1)
    			out << abs(temp->coefficient);
    
    		if (temp->exponent == 1)
    			out << "x";
    		else
    			out << "x^" << temp->exponent;
    
    		temp = temp->next;
    	}
    
    	return out;
    }
    Red parts are not recognised.

    Thanks in advance.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,083
    You could declare the function to be a friend, or you could move the code to a private print member function of Polynomial.
    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 ch4's Avatar
    Join Date
    Jan 2007
    Posts
    152
    But function is already friend.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,083
    No, you didn't declare it a friend of the inner class.
    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 ch4's Avatar
    Join Date
    Jan 2007
    Posts
    152
    Quote Originally Posted by laserlight View Post
    No, you didn't declare it a friend of the inner class.
    ok, i see.
    Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 08-02-2017, 04:15 AM
  2. Replies: 7
    Last Post: 11-18-2012, 11:17 AM
  3. Can Nested class access parent class's private member?
    By meili100 in forum C++ Programming
    Replies: 4
    Last Post: 06-05-2009, 08:42 AM
  4. Replies: 25
    Last Post: 10-29-2007, 04:08 PM
  5. derived class can not access base class protected member?
    By George2 in forum C++ Programming
    Replies: 2
    Last Post: 10-21-2007, 06:32 PM

Tags for this Thread