mam taki program wykorzystujący PIMPL:
Plik employee.h:
#ifndef EMPLOYEE_H
#define EMPLOYEE_H
#include <QSharedDataPointer>
#include <QString>
class EmployeeData;
class Employee
{
public:
Employee();
Employee(int id, QString name);
Employee(const Employee &other);
void setId(int id);
void setName(QString name);
int id() const;
QString name() const;
Employee& operator=(const Employee &other);
private:
QSharedDataPointer<EmployeeData> d;
};
#endif
Plik employee.cpp:
#include "employee.h"
class EmployeeData : public QSharedData
{
public:
EmployeeData() : id(-1) { name.clear(); }
EmployeeData(const EmployeeData &other)
: QSharedData(other), id(other.id), name(other.name) { }
~EmployeeData() { }
int id;
QString name;
};
Employee::Employee()
{
d = new EmployeeData;
}
Employee::Employee(int id, QString name)
{
d = new EmployeeData;
setId(id);
setName(name);
}
Employee::Employee(const Employee &other)
: d (other.d)
{
}
void Employee::setId(int id)
{
d->id = id;
}
void Employee::setName(QString name)
{
d->name = name;
}
int Employee::id() const
{
return d->id;
}
QString Employee::name() const
{
return d->name;
}
Employee &Employee::operator=(const Employee &other)
{
d=other.d;
return *this;
}
i main.cpp:
#include <QDebug>
#include "employee.h"
int main(int argc, char *argv[])
{
Employee e1(1001, "Albrecht Durer");
Employee e2 = e1;
e1.setName("Hans Holbein");
qDebug() << e1.name() << e2.name();
return 0;
}
przy próbie kompilacji, kompilator wywala taki błąd:
g++ -c -pipe -O2 -D_REENTRANT -Wall -W -DQT_NO_DEBUG -DQT_CORE_LIB -DQT_SHARED -I../../../../libqt/mkspecs/linux-g++ -I. -I../../../../libqt/include/QtCore -I../../../../libqt/include -I. -o main.o main.cpp
main.cpp:5: warning: unused parameter 'argc'
main.cpp:5: warning: unused parameter 'argv'
../../../../libqt/include/QtCore/qshareddata.h: In destructor 'QSharedDataPointer<T>::~QSharedDataPointer() [with T = EmployeeData]':
employee.h:10: instantiated from here
../../../../libqt/include/QtCore/qshareddata.h:90: error: invalid use of undefined type 'struct EmployeeData'
employee.h:7: error: forward declaration of 'struct EmployeeData'
../../../../libqt/include/QtCore/qshareddata.h:90: warning: possible problem detected in invocation of delete operator:
../../../../libqt/include/QtCore/qshareddata.h:90: warning: invalid use of undefined type 'struct EmployeeData'
employee.h:7: warning: forward declaration of 'struct EmployeeData'
../../../../libqt/include/QtCore/qshareddata.h:90: note: neither the destructor nor the class-specific operator delete will be called, even if they are declared when the class is defined.
make[1]: *** [main.o] Error 1
Może ktoś wie co jest źle?