Thread: Accessing protected class members

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Confused
    Join Date
    Nov 2002
    Location
    Warwick, UK
    Posts
    209

    Accessing protected class members

    Hello,

    I have a base class Paths, and a derived class LanguagePaths, which inherits Paths. There are various members of Paths, namely two doubles and a std::list <int>, which I'd like to copy from a Paths object into a LanguagePaths object.

    In order to do so, I've overloaded the operator= in LanguagePaths, as follows :

    Code:
    // LANGUAGE PATHS CLASS
    #ifndef _LANGUAGEPATHS_H
    #define _LANGUAGEPATHS_H
    
    //#include <list>
    #include <set>
    #include "paths.h"
    #include "shortcuts.h"
    
    class LanguagePaths : public Paths	// INHERITS PATHS
    {
    	public:
    		
    		// Constructor
    		LanguagePaths();
    				
    		// Accessors
    		bool banned(uint);		// Check if a production is banned
    		void insert(uint);		// Insert a ban
    		listitr marker();			// Marker position
    		listitr setmarker(listitr);	// Set the position of the marker
    				
    		// Overloaded operator
    		LanguagePaths operator= (const Paths &);
    		
    						
    	private:
    		
    		// Ban list
    		set <uint> ban;
    		
    		// Marker position
    		listitr markerpos;
    };
    
    #endif
    Code:
    // SNIPPET OF LANGUAGEPATHS.CPP
    // Overloading the equals operator
    LanguagePaths LanguagePaths::operator=(const Paths & original)
    {
    	// Set length and coefficients equal
    	length = original.length;
    	coefficient = original.coefficient;
    	
    	// Set path description to be the same
    	path = original.path;
    }
    Code:
    // PATHS CLASS
    #ifndef _PATHS_H
    #define _PATHS_H
    
    #include <list>
    #include "edge.h"
    #include "vertex.h"
    #include "graph.h"
    #include "shortcuts.h"
    using namespace std;
    
    
    
    
    class Paths
    {
    	public:
    		
    		// Constructor
    		Paths();
    		
    		// Accessors
    		uint back();			// Returns the ID of the vertex at the end of the path
    		uint front();			// Returns the ID of the vertex at the front of the path
    		int size();			// Returns the size of the path, in number of nodes
    		double getLength(Graph &);	// Returns the length of the path in spatial units
    		double getCoefficient(Graph &);	// Returns the trip coefficient of the path
    			
    		// Iterators
    		listitr begin();	// Returns an iterator to the beginning of the list
    		listitr end();		// Returns an iterator to the end of the list
    				
    		// Other methods
    		void push_back(uint);		// Add a vertex to the end of the path
    		bool contains(uint);		// Check if the path contains a certain vertex
    		void clear();			// Clear the path list completely
    		void incLength(double);		// Increase the length of the path
    		void incCoefficient(double);	// Increase the trip coefficient
    
    				
    	protected:
    		
    		double length;		// Path length
    		double coefficient;	// Path coefficient
    		list <uint> path;	// Path description
    };
    
    
    #endif


    I'm struggling to use my equals operator. I have a list <Paths> which contains path objects, and want to create a std::priority_queue <LanguagePaths>, like so :

    Code:
        // Create a priority queue using specified comparison function
        priority_queue < LanguagePaths, vector <LanguagePaths>, PathCompare > pq(comparefunction);
        
        // Create a temp LanguagePaths object
        LanguagePaths temp;
        
        for(list<Paths>::iterator i = finallist.begin(); i != finallist.end(); i++)
        {
        	temp = *i;
        	pq.push(temp);
        }

    I get these errors :
    Code:
    paths.h: In member function 'LanguagePaths LanguagePaths::operator=(const Paths&)':
    paths.h:45: error: 'double Paths::length' is protected
    I get the same error for Paths::coefficient and Paths::path.

    I was under the impression that protected members of a base class could be accessed by a derived class. I'm not quite sure what I should be doing.

    Any help would be greatly appreciated. Thanks very much !
    Quentin
    Last edited by Korhedron; 07-29-2010 at 06:20 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 08-04-2009, 11:40 AM
  2. Linking errors with static var
    By Elysia in forum C++ Programming
    Replies: 8
    Last Post: 10-27-2007, 05:24 PM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. accessing class variables
    By pug in forum C# Programming
    Replies: 3
    Last Post: 05-20-2005, 08:46 AM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM