![]() |
|
#1
|
|||
|
|||
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:
declarations.cpp CPP / C++ / C Code:
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
|
|||
|
|||
Re: Templates, Inheritance and Virtual Functions Problem!Quote:
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:
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:
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 GIDBlog
Problems with the Navy (Chiefs) by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
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