Click to See Complete Forum and Search --> : virtual functions with template parameters
nlgonsal
May 30th, 2002, 12:20 PM
Hi
I have virtual functions that use templates. However I get a linker error when I try to build my file. Does anyone know if Borland C++ Builder 5.0 can link virtual functions with templates as parameters ?
Thanks
Regards
Nitin
P.S. These are the error messages I get
[Linker Error]Unresolved external 'PLib::ParaSurface<float,3>::minDist2(const PLib::Point_nD<float,3>&, float&, float&, float, float, int, int, float, float, float, float)const' referenced from C:\BEN\UNIT1.OBJ. Unit1.obj is the main file where I have the declaration
NurbsSurface<float,3> a;
Paul McKenzie
May 30th, 2002, 10:43 PM
How is the virtual function defined? Is the definition of the template function within the CPP file? You can't instantiate a template without having the full source code present (at least this is the way with most compilers). In any respect, your question cannot be answered easily because we don't have your source code. But my feeling is that as long as the template function is defined within the file you are calling the function, there is no problem. It shouldn't matter whether the function is virtual or non-virtual. Also, the error may not have anything to do with templates. If just one of those parameters do not match up with the signature of the function, you will get an unresolved external error, regardless of whether or not you are using templates.
Why not do what most good programmers do: Write a small program to test this. You don't need the NURBS library to answer your question. When you are not sure what a compiler supports, you can easily answer your own question by writing a small app and see what happens. This is what all good programmers do when they have questions concerning whether a compiler or linker does or does not do something.
This not only makes it easier for you to answer the question, it also makes it easier for others to try your small test code to see if it makes sense. No one (or you can't expect anyone) to download the entire NURBS library to test this.
Regards,
Paul McKenzie
nlgonsal
May 31st, 2002, 09:31 AM
Hi
I did try my own litle app and it doesn't seem to work right for that as well. Also when I took out the virtual keyword, the file linked perfectly. So I'm beginning to believe that virtual functions do not work with template parameters.
What conclusion would you have come to?
Thanks
Regards
Nitin
Paul McKenzie
May 31st, 2002, 10:50 AM
Please post the code that you tried.
Regards,
Paul McKenzie
Paul McKenzie
May 31st, 2002, 11:02 AM
Originally posted by nlgonsal
Hi
I did try my own litle app and it doesn't seem to work right for that as well. Also when I took out the virtual keyword, the file linked perfectly. So I'm beginning to believe that virtual functions do not work with template parameters.
What conclusion would you have come to?
Thanks
Regards
Nitin I don't know for sure, but think about what a virtual function is and what the compiler does to implement dynamic binding.
When you declare a function virtual, the compiler builds a table of function pointers, where each function pointer points to the class's implementation of the virtual function. At runtime, the compiler checks the pointer type, and "magically" gets the correct function pointer and invokes the function. When you declare a template, the compiler cannot create a function pointer entry into this table, since a template is not a type. At compile time the compiler must know the type to build the v-table correctly.
Did you implement the virtual function that the compiler is complaining about explicitly in the template class itself? This is the only way I can think of that will force the compiler to see your definitions and therefore create a v-table entry for the function in question.
Regards,
Paul McKernzie
Anthony Mai
May 31st, 2002, 02:38 PM
All speculations that templates doesn't work with virtual functions, and any speculations about why it does not, are all totally wrong. If you disagree with this observation, you obviously doens't know what templates are.
Templates are macros under the pretence of C++. Templates are resolved at compile time as separate individual classes. So whatever a regular class can do, a template class can do it too.
If a regular class can define a virtual function and instatiate a vtable, so can a instantiated template class.
Nothing prevents one from declaring a virtual function in a template class. Whatever the original poster's problem is, it is a bug within his own code, not with the language.
Here is a simple program that demonstrates that it works.
// Test.cpp : This demonstrates that templates and virtual function CAN mix
// Copyright (c) 2002 Anthony Mai. All rights reserved.
#include <stdio.h>
template <class T>
class Base
{
friend int main (int argc, char* argv[]);
protected:
T m_data1;
public:
Base(T val) {m_data1 = val;}
~Base() {}
virtual T Add(T val) {m_data1 += val; return m_data1;}
};
template <class T>
class Derv : public Base<T>
{
friend int main (int argc, char* argv[]);
protected:
T m_data2;
public:
Derv(T val) : Base<T>(val), m_data2(val) {}
~Derv() {}
T Add(T val) {m_data2 = Base<T>::Add(val); return m_data2;}
};
int main(int argc, char* argv[])
{
Base<int>* pBase;
Base<int> myObj(0);
Derv<int> Obj2(3);
Base<double>* pfBase;
Base<double> fObj(0.1);
Derv<double> fObj2(0.3);
myObj.Add(2);
Obj2.Add(4);
pBase = (Base<int>*) &Obj2;
pBase->Add(5); // Which Add() it calls, when we use base class ptr?
if (Obj2.m_data2 == Obj2.m_data1)
{
printf("The virtual function of Derv<int> works OK\r\n");
}
else
{
printf("The virtual function of Derv<int> NOT working\r\n");
}
fObj.Add(0.2);
fObj2.Add(0.4);
pfBase = (Base<double>*) &fObj2;
pfBase->Add(0.5); // Which Add() it calls, when we use base class ptr?
if (fObj2.m_data2 == fObj2.m_data1)
{
printf("The virtual function of Derv<double> works OK\r\n");
}
else
{
printf("The virtual function Derv<double> NOT working\r\n");
}
return 0;
}
Paul McKenzie
May 31st, 2002, 04:38 PM
Wait until you see the code that the poster actually tried. I am well aware that you can have virtual functions within a template.
Regards,
Paul McKenzie
Anthony Mai
June 3rd, 2002, 02:41 PM
Granted templates in C++ are more complicate than macros in C. But the difference is merely quantitative, not qualitative. Templates are just "advanced" macros. Both are resolved at compile time. Actually both are resolved at the earliest pre-processing stage of compilation.
C macros are bad in many aspects that every one knows and I don't want to go into depth here. Templates are bad in a lot of things the same as macros.
I am unaware of the amount and the status of compiler bugs related to template implementation, because I stay away from templates. But its ironic that people who actively promote template usage are also those eager to remind us about how bad various compilers are when dealing with templates. If you can't trust the compiler to handle it reasonable well, then just avoid using the kind of code that compilers can't deal with.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.