Good evening, i am implementing code based on the following class diagram
I am obviously facing a circular dependency problem because Document and his children use pointer to Ambit and vice-versa; i've read resolved questions here on stack overflow and i followed this guide in the proper section but the code still shows problems, in particular doesn't found references to constructor and destructor. I want to point out that Document and Ambit are pure abstract class. To be clearer the code is as follow:
- Document.h
#ifndef DOCUMENT_H_
#define DOCUMENT_H_
class Ambit; //forward declaration
class Document{
public:
Document();
virtual ~Document();
virtual void addDocument(Document*);
virtual void showSelf();
virtual void showRelated();
virtual void setAnnotation(Ambit*);
};
#endif /* DOCUMENT_H_ */
- Ambit.h
#ifndef AMBIT_H_
#define AMBIT_H_
class Document; //forward declaration
class Ambit{
public:
Ambit();
virtual ~Ambit();
virtual void addAmbit(Ambit*);
virtual void showSelfA();
virtual void showRelatedA();
virtual void setAnnotationA(Document*);
};
#endif /* AMBIT_H_ */
- CompAmbit.h
#ifndef COMPAMBIT_H_
#define COMPAMBIT_H_
#include <iostream>
#include <vector>
#include "Ambit.h"
using namespace std;
class CompAmbit : public Ambit{
private:
string ambName;
Document* annotation;
vector<Ambit*> ambitVec;
public:
CompAmbit(string);
~CompAmbit();
void addAmbit(Ambit*);
void showSelfA();
void showRelatedA();
void setAnnotationA(Document*);
};
#endif /* COMPAMBIT_H_ */
- CompAmbit.cpp
#include<iostream>
#include<vector>
#include "CompAmbit.h"
#include "Document.h" //added header to Document for the knowledge of showSelf()
using namespace std;
CompAmbit::CompAmbit(string newName){ // @suppress("Class members should be properly initialized")
ambName = newName;
};
CompAmbit::~CompAmbit(){};
void CompAmbit::addAmbit(Ambit* newAmbit){
ambitVec.push_back(newAmbit);
};
void CompAmbit::setAnnotationA(Document* newAnnotation){
annotation = newAnnotation;
};
void CompAmbit::showSelfA(){
for(unsigned int i = 0; i<ambitVec.size(); i++ ){
ambitVec[i]->showSelfA();
};
};
void CompAmbit::showRelatedA(){
annotation->showSelf();
};
Section.h and Section.cpp are analogous to CompAmbit.
As you can see i need mutual references to call the function showSelf() (or showSelfA()) using the pointer in the annotation variable.
I've used forward declarations and i have added in CompAmbit.cpp the header to Document for the knowledge of showSelf(). I think i am acting good till now but as i compile i receive the following errors:
CompAmbit.cpp:14: undefined reference to Ambit::Ambit() //14 is line of CompAmbit constructor
CompAmbit.cpp:14: undefined reference to Ambit::~Ambit()
CompAmbit.cpp:18: undefined reference to Ambit::~Ambit() //18 is line of CompAmbit destructor
and analogous errors for Section.
Where am i doing wrong?
note: i have omitted leaf code to keep the question short.
EDIT: i forgot to say that errors pop up just after the addition of #include "Document.h" in CompAmbit.cpp
EDIT: by reading this post What is an undefined reference/unresolved external symbol error and how do I fix it? i figured out that i did not define constructor and destructor; for some reason i thought that pure virtual classes did not require any definition; i resolved just by replacing Ambit(); with Ambit(){};, same for the destructor and in the Document.h file.