Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #9102
| Path | csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!nx01.iad01.newshosting.com!newshosting.com!69.16.185.11.MISMATCH!npeer01.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!postnews.google.com!news4.google.com!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail |
|---|---|
| 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 | Sat, 20 Aug 2011 09:54:31 +1200 |
| Lines | 115 |
| Message-ID | <9b84coFr4mU3@mid.individual.net> (permalink) |
| References | <df70d9dd-0d6d-4c6f-96a6-4bcd2398d0ee@l2g2000vbn.googlegroups.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=ISO-8859-1; format=flowed |
| Content-Transfer-Encoding | 7bit |
| X-Trace | individual.net +OIDX2LBmDJO30uQCLKS+Qw5/NfeDQ3UfGltRiGOsK4B8n+W7H |
| Cancel-Lock | sha1:GIyS5w4G3MgaMZWWnbUkdZ32jts= |
| User-Agent | Mozilla/5.0 (X11; U; SunOS i86pc; en-US; rv:1.9.2.9) Gecko/20101021 Lightning/1.0b2 Thunderbird/3.1.4 |
| In-Reply-To | <df70d9dd-0d6d-4c6f-96a6-4bcd2398d0ee@l2g2000vbn.googlegroups.com> |
| Xref | x330-a1.tempe.blueboxinc.net comp.lang.c++:9102 |
Show key headers only | 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
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