I am relatively new at C++/QT and I can't get the following to compile.

Note the declaration of the operator<< overload in class Employee.  If instead 
I put the implementation here, it compiles.  But if the impmentation is 
placed outside of the class declaration as is done below, I get the following 
errors: (errors point to the implementation code)

(1) QDataStream& Employee::operator<<(QDataStream&, const Employee&)' must 
take exactly one argument
(2) `QDataStream& Employee::operator<<(QDataStream&, const Employee&)' member 
function declared in class `Employee'

What's wrong with the implementation?  It's driving me nuts.


****************
#include <iostream>
#include <qstring.h>
#include <qvaluelist.h>
#include <qdatastream.h>
#include <qfile.h>


class Employee
{
// DECLARATION
    friend QDataStream& operator<<(QDataStream &, const Employee &);

    public:
        Employee(): sn(0) {}
        Employee( const QString & forename, const QString & surname, int 
salary )
            : fn(forename), sn(surname), sal(salary)
        {}

        QString forename() const { return fn; }
        QString surname() const { return sn; }
        int salary() const { return sal; }
        void setSalary( int salary ) { sal = salary; }
    private:
        QString fn;
        QString sn;
        int sal;
};

// IMPLEMENTATION
QDataStream&  Employee :: operator<<(QDataStream &out, const Employee &emp) {
        out <<  emp.surname() << emp.forename() << emp.salary();
        return out;
}
        
int main () 
{
    typedef QValueList<Employee> EmployeeList;
    EmployeeList list;

    list.append( Employee("John", "Doe", 50000) );
    list.append( Employee("Jane", "Williams", 80000) );
    list.append( Employee("Tom", "Jones", 60000) );

    Employee mary( "Mary", "Hawthorne", 90000 );
    list.append( mary );
    mary.setSalary( 100000 );
    
    QFile file("test.out");
    file.open(IO_WriteOnly);
    QDataStream out(&file);
    out.setVersion(5);
    out << (Q_UINT32) 0x98c58f26;
    

    EmployeeList::iterator it;
    for ( it = list.begin(); it != list.end(); ++it )
        out << *it;
        //out << (*it).surname() << ", " <<
                //(*it).forename()<< " earns " <<
                //(*it).salary() <<"\n"; 
    file.close();
    
    return 0L;
}

____________________________________________________
Want to buy your Pack or Services from MandrakeSoft? 
Go to http://www.mandrakestore.com
Join the Club : http://www.mandrakeclub.com
____________________________________________________

Reply via email to