Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #9102

Re: Linker Error: Abstract base and 2 derived classes in singleton pattern

From Ian Collins <ian-news@hotmail.com>
Newsgroups comp.lang.c++
Subject Re: Linker Error: Abstract base and 2 derived classes in singleton pattern
Date 2011-08-20 09:54 +1200
Message-ID <9b84coFr4mU3@mid.individual.net> (permalink)
References <df70d9dd-0d6d-4c6f-96a6-4bcd2398d0ee@l2g2000vbn.googlegroups.com>

Show all headers | View raw


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

Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Linker Error: Abstract base and 2 derived classes in singleton pattern Vinesh S <vinesh2681@gmail.com> - 2011-08-19 14:26 -0700
  Re: Linker Error: Abstract base and 2 derived classes in singleton pattern red floyd <no.spam.here@its.invalid> - 2011-08-19 14:47 -0700
  Re: Linker Error: Abstract base and 2 derived classes in singleton pattern Ian Collins <ian-news@hotmail.com> - 2011-08-20 09:54 +1200
  Re: Linker Error: Abstract base and 2 derived classes in singleton pattern Paul N <gw7rib@aol.com> - 2011-08-20 08:58 -0700
    Re: Linker Error: Abstract base and 2 derived classes in singleton pattern Vinesh S <vinesh2681@gmail.com> - 2011-08-22 14:56 -0700

csiph-web