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


Groups > comp.lang.c++ > #9099 > unrolled thread

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

Started byVinesh S <vinesh2681@gmail.com>
First post2011-08-19 14:26 -0700
Last post2011-08-22 14:56 -0700
Articles 5 — 4 participants

Back to article view | Back to comp.lang.c++


Contents

  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

#9099 — Linker Error: Abstract base and 2 derived classes in singleton pattern

FromVinesh S <vinesh2681@gmail.com>
Date2011-08-19 14:26 -0700
SubjectLinker Error: Abstract base and 2 derived classes in singleton pattern
Message-ID<df70d9dd-0d6d-4c6f-96a6-4bcd2398d0ee@l2g2000vbn.googlegroups.com>
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 {
 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 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();
 protected:
       CRectangle();
       static CRectangle* rectanglePtr;
       CRectangle(CRectangle const&){};
       CRectangle& operator=(CRectangle&){};
 };

CRectangle* CRectangle::rectanglePtr = NULL;

class CTriangle: public CPolygon {
 public:
   int area (void)
     { return (width * height / 2); }

   static CTriangle* GetInstance()
     { if (!trianglePtr) trianglePtr = new CTriangle(); return
trianglePtr; }
 ~CTriangle();
 protected:
   CTriangle();
   static CTriangle* trianglePtr;
   CTriangle(CTriangle const&){}
   CTriangle& operator=(CTriangle&){};
 };

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;
}

so when i compile and link them

this is the error am looking at ....

[root@Krea-TV-DT1 IMGAdapter]# g++ -o test1.out test1.o
test1.o: In function `CRectangle::GetInstance()':
test1.cpp:
(.text._ZN10CRectangle11GetInstanceEv[CRectangle::GetInstance()]+0xa):
undefined reference to `CRectangle::rectanglePtr'
test1.cpp:
(.text._ZN10CRectangle11GetInstanceEv[CRectangle::GetInstance()]
+0x26): undefined reference to `CRectangle::CRectangle()'
test1.cpp:
(.text._ZN10CRectangle11GetInstanceEv[CRectangle::GetInstance()]
+0x47): undefined reference to `CRectangle::rectanglePtr'
test1.cpp:
(.text._ZN10CRectangle11GetInstanceEv[CRectangle::GetInstance()]
+0x4c): undefined reference to `CRectangle::rectanglePtr'
test1.o: In function `CTriangle::GetInstance()':
test1.cpp:
(.text._ZN9CTriangle11GetInstanceEv[CTriangle::GetInstance()]
+0xa): undefined reference to `CTriangle::trianglePtr'
test1.cpp:
(.text._ZN9CTriangle11GetInstanceEv[CTriangle::GetInstance()]
+0x26): undefined reference to `CTriangle::CTriangle()'
test1.cpp:
(.text._ZN9CTriangle11GetInstanceEv[CTriangle::GetInstance()]
+0x47): undefined reference to `CTriangle::trianglePtr'
test1.cpp:
(.text._ZN9CTriangle11GetInstanceEv[CTriangle::GetInstance()]
+0x4c): undefined reference to `CTriangle::trianglePtr'
collect2: ld returned 1 exit status

Please let me know as to what mistake am i doing .

Thanks,

Vinesh.S

[toc] | [next] | [standalone]


#9101

Fromred floyd <no.spam.here@its.invalid>
Date2011-08-19 14:47 -0700
Message-ID<j2mlns$vsu$1@dont-email.me>
In reply to#9099
On 8/19/2011 2:26 PM, 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:

General:
* Don't bother prefixing your classes with "C".  That's an
   MFC-ism and is generally unnecessary.
* void param lists are unnecessary.  Use void f(), instead of
   void f(void).


> //********************************************** test1.h
> *******************************//
> #include<iostream>
> using namespace std;
>
> class CPolygon {
>   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 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();
>   protected:
>         CRectangle();
>         static CRectangle* rectanglePtr;
>         CRectangle(CRectangle const&){};
>         CRectangle&  operator=(CRectangle&){};
>   };

Where are CRectangle() and ~CRectangle() defined?


This should probably not be in a header (though for this
toy example it works)
> CRectangle* CRectangle::rectanglePtr = NULL;
>

> class CTriangle: public CPolygon {
>   public:
>     int area (void)
>       { return (width * height / 2); }
>
>     static CTriangle* GetInstance()
>       { if (!trianglePtr) trianglePtr = new CTriangle(); return
> trianglePtr; }
>   ~CTriangle();
>   protected:
>     CTriangle();
>     static CTriangle* trianglePtr;
>     CTriangle(CTriangle const&){}
>     CTriangle&  operator=(CTriangle&){};
>   };
>

This should probably not be in a header (though for this
toy example it works)
> CTriangle* CTriangle::trianglePtr = NULL;

Similarly, where are CTriangle() and ~CTriangle()
defined()


>
> //******************************************* 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;
> }

I don't see what's wrong with the static members,
but you do have to declare those missing constructors
and destructors.

[toc] | [prev] | [next] | [standalone]


#9102

FromIan Collins <ian-news@hotmail.com>
Date2011-08-20 09:54 +1200
Message-ID<9b84coFr4mU3@mid.individual.net>
In reply to#9099
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

[toc] | [prev] | [next] | [standalone]


#9114

FromPaul N <gw7rib@aol.com>
Date2011-08-20 08:58 -0700
Message-ID<ec0c6a4c-7eb9-4b27-9a85-038e20d6eaae@i9g2000yqk.googlegroups.com>
In reply to#9099
On Aug 19, 10:26 pm, Vinesh S <vinesh2...@gmail.com> 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.

Others have answered this, but I thought I might make a general
comment. If you code that is not working, try cutting it down to
simpler stuff and see if that works. That can help narrow down the
problem, and also may give you a shorter piece of code to post if you
still need to. For instance, with your code, the problem wasn't that
you were deriving *two* classes; you could have tried deriving just
one and that would have given you a shorter piece of code to post.
Indeed, the problem wasn't with the deriving itself - I think you
would have had the same problem with a straightforward singleton.

But this is just a comment as to how to do things even beter - your
post was very good especially for a first post.

Hope this helps.
Paul.

[toc] | [prev] | [next] | [standalone]


#9209

FromVinesh S <vinesh2681@gmail.com>
Date2011-08-22 14:56 -0700
Message-ID<c617d58f-f3df-44db-bdb9-459a1ef16014@a4g2000yqg.googlegroups.com>
In reply to#9114
Thanks All of you for the responses.
Helped me to solve my problem much.

Vinesh.S


On Aug 20, 10:58 am, Paul N <gw7...@aol.com> wrote:
> On Aug 19, 10:26 pm, Vinesh S <vinesh2...@gmail.com> 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.
>
> Others have answered this, but I thought I might make a general

> comment. If you code that is not working, try cutting it down to
> simpler stuff and see if that works. That can help narrow down the
> problem, and also may give you a shorter piece of code to post if you
> still need to. For instance, with your code, the problem wasn't that
> you were deriving *two* classes; you could have tried deriving just
> one and that would have given you a shorter piece of code to post.
> Indeed, the problem wasn't with the deriving itself - I think you
> would have had the same problem with a straightforward singleton.
>
> But this is just a comment as to how to do things even beter - your
> post was very good especially for a first post.
>
> Hope this helps.
> Paul.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c++


csiph-web