GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 03-Mar-2009, 00:26
paranoicpum paranoicpum is offline
New Member
 
Join Date: Mar 2009
Posts: 1
paranoicpum is on a distinguished road
Unhappy

Templates, Inheritance and Virtual Functions Problem!


I'm having problems with this scenario:

I have a Class A, it has a virtual function.
I also have a Class B, this class inherits from A and have a template.

header.hh
CPP / C++ / C Code:
class A
{
..
..
//class As methods
virtual void f()....
}



template<class T>
class B:public A
{
...
//class Bs methods
}


declarations.cpp
CPP / C++ / C Code:

void B<T>::f() {..}

//others A and B methods..


error:
declarations.cpperror: no ‘void B<T>::f()’ member function declared in class ‘B<T>’|

so I would like to overwrite the virtual function inherited in class B,
but I got an error telling me that the virtual function wasn't declared,
and I indeed didn't declare it ,since it is supposed to be inherited.
http://www.parashift.com/c++-faq-lite/templates.html#faq-35.19
there I found my problem...
I wonder if anybody has a suggestion in fixing my problem or being able to reach the virtual function from B in order to overwrite it.

thanks
  #2  
Old 08-Mar-2009, 01:11
Kimmo Kimmo is offline
Member
 
Join Date: Mar 2007
Location: Finland
Posts: 285
Kimmo is a jewel in the roughKimmo is a jewel in the roughKimmo is a jewel in the rough

Re: Templates, Inheritance and Virtual Functions Problem!


Quote:
Originally Posted by paranoicpum
If it's not too late...

I don't think your problem is that complicated. Notice in that example both classes are templates, you have only one of them as a template. I think your problem is simply in the syntax.

I made this simple program to test.

CPP / C++ / C Code:
#include <iostream>

class BaseClass
{
public:

    BaseClass(int amount);
    
    virtual int amount();
    // doubles the value of m_amount
    virtual void doubleAmount();
    virtual void doubleAmount(int times);
    virtual void tripleAmount();
    virtual void tripleAmount(int times);
    
private:

    int m_amount;
    
};


template <typename Type>
class DerivedClass : public BaseClass
{
public:

    DerivedClass(int amount);
    
    void doubleAmount();
    void doubleAmount(int times);
//    void tripleAmount();
    int amount();
    
private:

    Type m_variable;
    
};
  
//
// BaseClass methods
//
  
BaseClass::BaseClass(int amount)
    : m_amount(amount)
{



}

void BaseClass::doubleAmount()
{
    m_amount *= 2;
}

void BaseClass::doubleAmount(int times)
{
    for (int i = 0; i < times; ++i)
    {
        m_amount *= 2;
    }
}

void BaseClass::tripleAmount()
{
    m_amount *= 3;
}

void BaseClass::tripleAmount(int times)
{
    for (int i = 0; i < times; ++i)
    {
        m_amount *= 3;
    }
}

int BaseClass::amount()
{
    return m_amount;
}

//
// DerivedClass methods
//

template <typename Type>
DerivedClass<Type>::DerivedClass(int amount)
    : BaseClass(amount), m_variable(amount)
{



}

template <typename Type>
void DerivedClass<Type>::doubleAmount()
{
    m_variable = m_variable * 2;
}

template <typename Type>
void DerivedClass<Type>::doubleAmount(int times)
{
    for (int i = 0; i < times; ++i)
    {
        m_variable *= 2;
    }
}

/*
template <typename Type>
void DerivedClass<Type>::tripleAmount()
{
    m_variable *= 3;
}
*/

template <typename Type>
int DerivedClass<Type>::amount()
{
    return m_variable;
}


int main()
{
    using std::cout;
    using std::endl;

    DerivedClass<int> object(5);
    
    cout << "value: " << object.amount();
    cout << endl;
    
    object.doubleAmount();
    
    cout << "After calling object.doubleAmount(): ";
    cout << object.amount();
    cout << endl;
    
    object.doubleAmount(4);
    
    cout << "After calling object.doubleAmount(4): ";
    cout << object.amount();
    cout << endl;
       
    object.tripleAmount(3);
    
    cout << "After calling object.tripleAmount(3): ";
    cout << object.amount();
    cout << endl;
       
    return 0;
}


There's BaseClass with virtual methods. Then there's the templated DerivedClass, which derives from BaseClass. In DerivedClass I have overwritten some virtual methods found in BaseClass. Notice how I include their declarations in the class declaration?

Then I can start their definitions with
CPP / C++ / C Code:
template <typename Type>
int DerivedClass<Type>::amount()

As far as I see, this scenario is exactly like what you have presented. If I uncomment the method definition starting from line 105, I get an error similar to yours.

So, if you want to overwrite some methods, you need to declare them in the class declaration.

I also remembered that if you overwrite a virtual method from a base class, you need to overwrite all the other overloaded methods of the same name. I include a method void tripleAmount() in the example to demostrate.

There's a call to tripleAmount() at the end of the example. It calls the BaseClass version of tripleAmount and modifies the BaseClass variable m_amount, so it doesn't have any effect on the output shown. However, if you uncomment both the tripleAmount() declaration and definition from DerivedClass, you will get an error stating there's no matching function. That's because the overwrite of tripleAmount() also hid the other, tripleAmount(int) method.

Hope it helps.
 
 

Recent GIDBlogProblems with the Navy (Chiefs) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Virtual multiple inheritance: constructors order natasha_u C++ Forum 2 25-Jul-2007 03:26
Inheritance Suspicious Problem Peter_APIIT C++ Forum 12 15-May-2007 23:01
Class inheritance problem Sita C++ Forum 11 07-Apr-2006 11:20
Inheritance problem CaptnB C++ Forum 2 27-Jan-2005 09:09

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 20:51.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.