Re: Linker Error: Abstract base and 2 derived classes in singleton
pattern
On 08/20/11 09:26 AM, Vinesh S wrote:
Hello all,
This is my first message to the group.
I have a question where i derive 2 child classes which are of
singleton pattern from an abstract base class.
Code:
//********************************************** test1.h
*******************************//
#include<iostream>
using namespace std;
class CPolygon {
Why not just class Polygon? The 'C' is just noise.
protected:
int width, height;
public:
CPolygon(){};
virtual ~CPolygon(){};
void set_values (int a, int b)
{ width=a; height=b; }
virtual int area (void) = 0;
(void) is a Cism best not used in C++ code.
void printarea (void)
{ cout<< this->area()<< endl; }
};
class CRectangle: public CPolygon {
public:
int area (void)
{ return (width * height); }
static CRectangle* GetInstance()
{ if (!rectanglePtr) rectanglePtr = new CRectangle(); return
rectanglePtr; }
~CRectangle();
This isn't defined anywhere.
protected:
CRectangle();
This isn't defined anywhere.
static CRectangle* rectanglePtr;
CRectangle(CRectangle const&){};
CRectangle& operator=(CRectangle&){};
This shouldn't compile, missing return (also superfluous semi-colon).
};
CRectangle* CRectangle::rectanglePtr = NULL;
This shouldn't be in a header, static members must be defined once and
only once. Put the definition in a cpp file.
class CTriangle: public CPolygon {
public:
int area (void)
{ return (width * height / 2); }
static CTriangle* GetInstance()
{ if (!trianglePtr) trianglePtr = new CTriangle(); return
trianglePtr; }
~CTriangle();
This isn't defined anywhere.
protected:
CTriangle();
This isn't defined anywhere.
static CTriangle* trianglePtr;
CTriangle(CTriangle const&){}
CTriangle& operator=(CTriangle&){};
This shouldn't compile, missing return.
};
CTriangle* CTriangle::trianglePtr = NULL;
//******************************************* test1.cpp
*******************//
#include "test1.h"
int main (void)
{
CPolygon *ppoly1 = CRectangle::GetInstance();
CTriangle *ppoly2 = CTriangle::GetInstance();
ppoly1->set_values (4,5);
ppoly2->set_values (4,5);
ppoly1->printarea();
ppoly2->printarea();
delete ppoly1;
delete ppoly2;
return 0;
}
Please let me know as to what mistake am i doing .
Try the fixes I suggested and it should link.
--
Ian Collins