I'm curently learning inheritance and polymorphism, and coding this code. The problem is that when I execute this code I'm getting this error
Undefined reference to test::Reference::clone() const
Can you please help me understand why I'm getting this error and if possible tell me if there is any other error in my code. I already spend 4 days trying to make it work.
//Main.cpp
#include <iostream>
#include <string>
#include "Biblio.h"
#include "Reference.h"
#include "Book.h"
using namespace std;
using namespace test;
int main ()
{
Biblio aBiblio ("biblio");
Book aBook ("autor", "title", 2011, "id", "editor", "city");
cout << aBook.reqFormatedReference () << endl;
aBiblio.addReference (aBook);
Book anotherBook ("autor", "title", 2011, "id", "editor", "city");
cout << anotherBook.reqFormatedReference () << endl;
aBiblio.addReference (anotherBook );
cout << aBiblio.reqFormatedBiblio () << endl;
return 0;
}
//Biblio.h
#ifndef BIBLIO_H
#define BIBLIO_H
#include <vector>
#include "Reference.h"
namespace test
{
class Biblio
{
public:
Biblio (const std::string& p_nameBiblio);
~Biblio ();
Biblio (const Biblio& rhs);
std::string reqNameBiblio () const;
void addReference (const Reference& p_newReference);
virtual std::string reqFormatedBiblio () const;
private:
std::string m_nameBiblio;
std::vector<Reference*> m_vReferences;
void emptyVector ();
};
} /* namespace tes*/
#endif /* BIBLIO_H */
//Biblio.cpp
#include "Bibliographie.h"
#include <vector>
#include <iostream>
#include <sstream>
using namespace std;
namespace test
{
Biblio::Biblio (const std::string& p_nameBiblio) : m_nameBiblio (p_nameBiblio){}
string Biblio::reqNameBiblio () const
{
return m_nameBiblio;
}
void Biblio::addReference(const Reference& p_newReference)
{
m_vReferences.push_back(p_newReference.clone());
}
string Biblio::reqFormatedBiblio () const
{
ostringstream os;
for (auto x : m_vReferences)
{
int i = 1;
os << reqNameBiblio() << endl << "===============================" << "[" << i << "]" << x;
i++;
}
return os.str ();
}
void Biblio::emptyVector()
{
while (!m_vReferences.empty ())
{
m_vReferences.pop_back ();
}
}
Biblio::~Biblio ()
{
emptyVector() ;
}
} /* namespace tes */
//Reference.h
#ifndef REFERENCE_H
#define REFERENCE_H
namespace test
{
class Reference
{
public:
Reference ();
Reference (const std::string& p_autor, const std::string& p_title, int p_year, const std::string& p_id);
virtual ~Reference () { };
std::string reqAutor () const;
std::string reqTitle () const;
int reqYear () const;
std::string reqid () const;
virtual std::string reqFormatedReference () const;
virtual Reference* clone () const;
private:
std::string m_autor;
std::string m_title;
int m_year;
std::string m_id;
};
} /* namespace test*/
#endif /* REFERENCE_H */
//Reference.cpp
#include "Reference.h"
#include <iostream>
#include <sstream>
using namespace std;
namespace test
{
Reference::Reference (const std::string& p_autor, const std::string& p_title, int p_year, const std::string& p_id) :
m_autor (p_autor), m_title (p_title), m_year (p_year), m_id (p_id){}
string Reference::reqAutor () const
{
return m_autor;
}
string Reference::reqTitle () const
{
return m_title;
}
int Reference::reqYear () const
{
return m_year;
}
string Reference::reqid () const
{
return m_id;
}
string Reference::reqFormatedReference () const
{
ostringstream os;
os << reqAutor () << ". " << reqTitle () << "." << endl;
return os.str ();
}
}/* namespace test*/
//Book.h
#ifndef BOOK_H
#define BOOK_H
#include "Reference.h"
namespace test
{
class Book: public Reference
{
public:
Book (const std::string& p_autor, const std::string& p_title, int p_year, const std::string& p_id, const std::string& p_editor, const std::string& p_city);
std::string reqEditor () const;
std::string reqCity () const;
virtual std::string reqFormatedReference () const;
virtual Reference* clone () const;
private:
std::string m_editor;
std::string m_city;
};
} /* namespace test*/
#endif /* BOOK_H */
//Book.cpp
#include "Book.h"
#include <iostream>
#include <sstream>
using namespace std;
namespace test
{
Book::Book(const std::string& p_autor,
const std::string& p_title,
int p_annee,
const std::string& p_id,
const std::string& p_editor,
const std::string& p_city) :
Reference (p_autor, p_title, p_year, p_id), m_editor (p_editor), m_city (p_city){}
string Book::reqEditor () const
{
return m_editor;
}
string Book::reqCity () const
{
return m_city;
}
string Book::reqFormatedReference () const
{
ostringstream os;
os << Reference::reqFormatedReference () << " " << reqCity () << " : " << reqEditor () << ", "
<< Reference::reqYear () << ". " << Reference::reqid () << "." << endl;
return os.str ();
}
Reference* Book::clone () const
{
return new Book (*this);
}
} /* namespace test*/
Aucun commentaire:
Enregistrer un commentaire