Inheritance + Template

This is a discussion on Inheritance + Template within the C++ Programming forums, part of the General Programming Boards category; Hello, I'm new in C++ :P ... Btw I have some problem with inheritance and templates... Code: class A { ...

  1. #1
    Ugly C Lover audinue's Avatar
    Join Date
    Jun 2008
    Location
    Indonesia
    Posts
    489

    Question Inheritance + Template

    Hello, I'm new in C++ :P ...
    Btw I have some problem with inheritance and templates...

    Code:
    class A
    {
        public:
            void testA()
            {
                printf("Hello from A\n");
            };
    };
    
    class B : private A
    {
        public:
            void testB()
            {
                testA();
                printf("Hello from B\n");
            };
    };
    Is OK!

    Code:
    template<typename T>class A
    {
        public:
            void testA()
            {
                printf("Hello from A\n");
            };
    };
    
    template<typename T>class B : private A // <--- is it right???
    {
        public:
            void testB()
            {
                testA(); // <--- ???
                printf("Hello from B\n");
            };
    
    };
    But, how do I do this??

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Code:
    template<typename T>class B : private A<T>

  3. #3
    Ugly C Lover audinue's Avatar
    Join Date
    Jun 2008
    Location
    Indonesia
    Posts
    489
    Code:
    D:\Documents and Settings\Audi Nugraha\Desktop\jancok.cpp: In member function `void B<T>::testB()':
    D:\Documents and Settings\Audi Nugraha\Desktop\jancok.cpp:17: error: there are no arguments to `testA' that depend on a template parameter, so a declaration of `testA' must be available

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    It looks like you might call testA like this:

    Code:
    this->testA();
    A<T>::testA();
    although I'm not sure why it thinks testA is not declared. I guess these forms force the compiler to look deeper into A...
    Last edited by anon; 07-19-2008 at 11:52 PM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Ugly C Lover audinue's Avatar
    Join Date
    Jun 2008
    Location
    Indonesia
    Posts
    489
    I think I should post the whole source code >_<" Somebody help me please... this is a very basic thing, isn't it?
    Code:
    template<typename T>class MemoryPool
    {
        private:
    
            T *base;
            unsigned short initial;
            unsigned short increment;
            unsigned int allocated;
    
        public:
    
            MemoryPool(unsigned short size)
            {
                base = NULL;
    
                if((base = (T *)malloc(size * sizeof(T))) == NULL)
                    abort();
    
                initial     = size;
                increment   = 1;
                allocated   = size;
            };
    
            ~MemoryPool()
            {
                free(base);
            };
    
            T *check(unsigned int offset)
            {
                while(offset >= allocated)
                {
                    if((base = (T *)realloc(base, (allocated = ++increment * initial) * sizeof(T))) == NULL)
                        abort();
                }
    
                return base;
            };
    
            T *getBase()
            {
                return base;
            };
    };
    
    template<typename T>class Stack : private MemoryPool<T>
    {
        private:
            unsigned int offset;
    
        public:
    
            Stack(unsigned short size)
         // ^---^ BAD?
            {
                //BAD: this->MemoryPool(size);
                //BAD: (MemoryPool)this->MemoryPool(size);
                //BAD: MemoryPool::MemoryPool(size);
            };
    };
    
    
    int main(int argc, char **argv)
    {
        Stack<char> stk(10); // <--- error
    
        getchar();
    
        return 0;
    }
    Oops and the error:
    Code:
    D:\Documents and Settings\Audi Nugraha\Desktop\stack.cpp: In constructor `Stack<T>::Stack(short unsigned int) [with T = char]':
    D:\Documents and Settings\Audi Nugraha\Desktop\stack.cpp:68:   instantiated from here
    D:\Documents and Settings\Audi Nugraha\Desktop\stack.cpp:58: error: no matching function for call to `MemoryPool<char>::MemoryPool()'
    D:\Documents and Settings\Audi Nugraha\Desktop\stack.cpp:5: note: candidates are: MemoryPool<char>::MemoryPool(const MemoryPool<char>&)
    D:\Documents and Settings\Audi Nugraha\Desktop\stack.cpp:16: note:                 MemoryPool<T>::MemoryPool(short unsigned int) [with T = char]
    Last edited by audinue; 07-20-2008 at 12:26 AM.

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Base class constructor is specified in initializer list:

    Code:
            Stack(unsigned short size):
                MemoryPool<T>(size)
            {
                //BAD: this->MemoryPool(size);
                //BAD: (MemoryPool)this->MemoryPool(size);
                //BAD: MemoryPool::MemoryPool(size);
            };
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Specialising a member function with a template template parameter
    By the4thamigo_uk in forum C++ Programming
    Replies: 10
    Last Post: 10-12-2007, 04:37 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. error: template with C linkage
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 08:11 AM
  4. Template and Inheritance
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 04-18-2002, 01:45 PM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 11:54 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21