Entra

View Full Version : [C++] Undefined reference su ctor/dtor e metodi


error 404
25-11-2011, 17:43
Salve ragazzi,
sto lavorando ad un progettino (allegato in maniera completa infondo) simil-tomboy con pattern observer (ridotto all'osso).
Quando vado a compilare mi da un "undefined reference.."

build/Debug/GNU-Linux-x86/nota.o: In function `Nota':
/home/giovanni/NetBeansProjects/tomboy2/nota.cpp:7: undefined reference to `Subject::~Subject()'
/home/giovanni/NetBeansProjects/tomboy2/nota.cpp:18: undefined reference to `Subject::~Subject()'
build/Debug/GNU-Linux-x86/nota.o: In function `~Nota':
/home/giovanni/NetBeansProjects/tomboy2/nota.cpp:23: undefined reference to `Subject::~Subject()'
/home/giovanni/NetBeansProjects/tomboy2/nota.cpp:23: undefined reference to `Subject::~Subject()'
build/Debug/GNU-Linux-x86/nota.o:(.rodata._ZTV4Nota[vtable for Nota]+0x10): undefined reference to `Nota::registerObserver(Observer*)'
build/Debug/GNU-Linux-x86/nota.o:(.rodata._ZTV4Nota[vtable for Nota]+0x14): undefined reference to `Nota::removeObserver(Observer*)'
build/Debug/GNU-Linux-x86/nota.o:(.rodata._ZTV4Nota[vtable for Nota]+0x18): undefined reference to `Nota::notifyObservers() const'


I file da cui penso dipenda il problema sono:
nota.h
#ifndef NOTA_H
#define NOTA_H

#include "observer.h"
#include "subject.h"
#include <iostream>
#include <list>
#include <string>

using namespace std;

class Nota : public Subject {
public:
Nota();
Nota(string titolo, string testo);

~Nota();

void stampaTitolo();
void stampaTesto();
void registerObserver( Observer* observer );
void removeObserver( Observer* observer );
void notifyObservers() const;

private:
string _titolo, _testo;
};

#endif
nota.cpp
#include "nota.h"
#include "observer.h"
#include "subject.h"
#include <iostream>
#include <string>

Nota::Nota(){
string titolo;
string testo;
cout << "Inserisci titolo: " << endl;
getline(cin,titolo);
_titolo = titolo;
cout << "Scrivi il testo della nota: " << endl;
getline(cin,testo);
_testo = testo;
}

Nota::Nota(string titolo, string testo){
_titolo = titolo;
_testo = testo;
}

Nota::~Nota(){
cout << "Nota '" << _titolo << "' cancellata" << endl;
}

void Nota::stampaTitolo(){
cout << _titolo << endl;
}

void Nota::stampaTesto(){
cout << _testo << endl;
}

void Nota::registerObserver(Observer* observer){}
void Nota::removeObserver(Observer* observer){}
void Nota::notifyObservers() const{}
subject.h
#ifndef SUBJECT_H
#define SUBJECT_H

#include "observer.h"

class Subject{
protected:
virtual ~Subject() = 0;

public:
virtual void registerObserver( Observer* observer ) = 0;
virtual void removeObserver( Observer* observer ) = 0;
virtual void notifyObservers() const = 0;
};

#endif

Soprattutto non capisco quel riferimento al distruttore virtuale...

Sapete aiutarmi? grazie ciao

tomminno
25-11-2011, 21:54
Soprattutto non capisco quel riferimento al distruttore virtuale...

Sapete aiutarmi? grazie ciao

Beh hai definito un distruttore astratto!
Il linker si lamenta che non riesce a trovarne una implementazione.

I distruttori non possono essere astratti, virtuali si ma una implemerntazione la devono avere!

Poi una osservazione: Subject è una interfaccia (definisce tutti metodi astratti), sarebbe meglio che il distruttore fosse pubblico.

Infine evita l'utilizzo dello using nei file header.