Topic: Template to Self


Author: rbach@bnr.ca (Richard Bach)
Date: 9 Aug 1993 20:46:58 GMT
Raw View
When I compile the template class foo with the class bar listed below
using USL Cfront 3.0 compiler on an HP827 Business server running
HP-UX 9.0 I receive the following error messasge

   CC: "test.C", line 32: internal error: unknown message (4200)


   CC: "test2.C", line 8: error: member print undefined (1287)
   "test2.C", line 8:       error detected during the\
     instantiation offoo <bar>
   "test2.C", line 17:     is the site of the instantiation

In class bar I am trying to instantiate a foo<bar>* object.  Is this
permitted in C++ and my compiler is wrong?  Or am I wrong and my
compiler is right?  It isn't obvious to me from the ARM if this is a
correct usage of the template mechanism.

I would appreciate it if you could email your answers to me.  Thanks.

+--------------------------------+------------------------+
| Richard Bach              _    |   phone: 214-684-5341  |
| BNR Inc.,  M/S DO307    _| ~_  |   fax:   214-684-3716  |
| P.O. Box 833871         \,  _} |   esn:   444-5341      |
| Richardson, Texas 75083   \(   |   email: rbach@bnr.ca  |
+--------------------------------+------------------------+

..................................................................

//test.C

#include <iostream.h>
template <class T>
class foo
{
  T* pObj;
  void (T::*pMem)();
 public:
  foo(T* pObj_,void (T::*pMem_)() ): pObj(pObj_), pMem(pMem_) {}
  void go() {(pObj->*pMem)();}
};

class bar
{
  foo<bar>* pFoo;
 public:
  bar(): pFoo(new foo<bar>(this, &bar::print)) {}
  void print();
  void func()  {pFoo->go();}
};

void bar::print()
{
  cout << "I'm a bar\n";
}


main()
{
  bar b;
  b.func(); // should call print();
  return 0;
}

..................................................................

//test2.C

#include <iostream.h>
template <class T>
class foo
{
  T* ptr;
 public:
  foo(T* ptr_): ptr(ptr_) {}
  void go() {ptr->print();}
};

class bar
{
 public:
  bar(): pFoo(new foo<bar>(this)) {pFoo->go();}
  void print() {cout << "I'm a bar.\n";}
 private:
  foo<bar>* pFoo;
};


main()
{
  bar b;
  return 0;
}