Thread: overloading operator <<

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    134

    overloading operator <<

    hi, i overloaded the operator << as a friend function in this class, and when i compile the program i get this error:-
    \customerType.cpp(90): error C2065: 'vtl' : undeclared identifier

    as a friend function doesnt it have access to all private/protected members. Here is the code......what am i doing wrong?

    customerType.h

    Code:
    #ifndef H_CUSTOMERTYPE
    #define H_CUSTOMERTYPE
    
    #include<iostream>
    #include<string>
    #include<list>
    
    using namespace std;
    
    class customerType
    {
    	friend ostream& operator<<(ostream&, const customerType&);
    protected:
    	string name;
    	int numCheckedOut;
    	list<string> vtl;
    
    public:
    	bool operator < (const customerType& )const;//defined
    	bool operator == (const customerType& )const;//defined
    	void printCustomerName()const;//defined
    	void addTitle(string);
    	void removeTitle(string);
    	bool matchName(string);
    	void printCustomerTitles();
    	void setName(string);
    	void setNumCheckedOut(int);
    	customerType(string);
    	customerType();
    	void removeAllTitles();
    	string getName()const;
    	int numberChecked()const;
    };
    
    #endif
    customerType.cpp

    Code:
    #include "customerType.h"
    #include<list>
    #include<iostream>
    
    using namespace std;
    
    bool customerType::operator < (const customerType& other)const
    {
    	return (name < other.name);
    }
    
    bool customerType::operator == (const customerType& other)const
    {
    	return (name == other.name);
    }
    
    void customerType::printCustomerName()const
    {
    	cout<<"Customer Name: "<<name<<endl;
    }
    
    void customerType::addTitle(string mName)
    {
    	vtl.push_back(mName);
    	numCheckedOut++;
    }
    
    void customerType::removeTitle(string mName)
    {
    	vtl.remove(mName);
    	numCheckedOut--;
    }
    
    bool customerType::matchName(string Cname)
    {
    	return( name == Cname );
    }
    
    void customerType::printCustomerTitles()
    {
    	list<string>::const_iterator video;
    
    	for(video=vtl.begin();video!=vtl.end();video++)cout<<"Movie Name: " << *video<<endl;
    }
    
    void customerType::setName(string Cname)
    {
    	name=Cname;
    }
    
    void customerType::setNumCheckedOut(int num)
    {
    	numCheckedOut=num;
    }
    
    customerType::customerType(string cName)
    {	
    	name=cName;
    	numCheckedOut=0;
    	
    }
    
    customerType::customerType()
    {
    	name="";
    	numCheckedOut=0;
    }
    
    void customerType::removeAllTitles()
    {
    	for(int i=0;i<numCheckedOut;i++)vtl.pop_back();
    	numCheckedOut=0;
    }
    
    string customerType::getName()const
    {
    	return name;
    }
    
    int customerType::numberChecked()const
    {
    	return numCheckedOut;
    }
    
    ostream& operator<<(ostream& os, const customerType& customer)
    {
    	list<string>::const_iterator location;
    	os<<customer.name;
    	os<<customer.numCheckedOut;
    	for(location= vtl.begin();location!=vtl.end();location++)
    	{
    		os<<*location<<endl;
    	}
    	return os;
    }

  2. #2
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    I think it needs to be customer.vtl in order for you to access that variable.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    by default all members of a class are private. Therefore the friend function you have declared has private access, meaning it can only be accessed by a public member function. I would try declaring the friend function with public access so it is available to any function.

  4. #4
    Amateur
    Join Date
    Sep 2003
    Posts
    228
    joshdick is right, a friend function does have acces to members even if they are privates or protected. But they are not members thus they don't have a this pointer.

    elad, I think what you are saying is wrong; as friends do not have the class scope, they do not have the access limitations of the class neither.

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    I agree with the customer.vtl requirement.

    With re friend functions I know friend functions don't have access to the this ponter of object to which they are a friend, just access to the data members, but I don't know about scope. Never thought of it actually because I've never seen a friend function declared as private, they've always been public so I just assumed that they were declared so for the same reason other methods are declared public. Live and learn.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Overloading operators
    By ugmusicbiz in forum C++ Programming
    Replies: 2
    Last Post: 02-13-2009, 01:41 PM
  2. unary operator overloading and classes
    By coletek in forum C++ Programming
    Replies: 9
    Last Post: 01-10-2009, 02:14 AM
  3. Overloading operator ==
    By anon in forum C++ Programming
    Replies: 4
    Last Post: 05-10-2006, 03:26 PM
  4. operator overloading
    By blue_gene in forum C++ Programming
    Replies: 6
    Last Post: 04-29-2004, 04:06 PM
  5. overloading
    By theLukerBoy in forum C++ Programming
    Replies: 6
    Last Post: 11-04-2002, 08:49 PM