I have one templated class in one test1.h file.
template<Data_T>
class test
{
.....
.....
}
In another header file test2.h i am creating a structure
like below
struct example
{
int a,
float b,
test (*fun_ptr)(const test &a);
};
But compiler gives the error as "test" not declared.I have included the test1.h file in test2.h.
I have mentioned it like that only. I forgot to mention that in my example. Means i have declared my class as
template<typename Data_T>
class test
{
.....
.....
}
But in structure i don't want to specify the type(as mentioned in above post "int") because i want it to be decided at runtime.
Actuallt the whole logic or implementation is below
in "test" class(templated) there is a function like
template<typename Data_T >
test<Data_T>
test<Data_T>::Convert(const Unit &unit)
{
......
......
func1() //this function resides in another .cpp file whose corresponding file is test2.h where that "example" structure is defined
}
convert function is called upon a "test" object.
Now in func1,
func1(...,...)
{
check for the mapping in the table of type "example",wherever match is found call the corresponding "fun_ptr" function
}
table is of type "example"
const example table[] =
{
{1,2,f1},
(2,3,f2},
.........
};
i want to create a templated function pointer, and i don't want to supply the type as suggested by "Seraphimsan". Because i don't want to restrict the function pointer at compile time. Whenever user creates a object of any type(int,float, complex) then this function should be invoked.
That's true but the whole idea to provide template function or class is that you generalize the class or function so that whatever type of object is created of a template class that version of the object is created of the template class. Tht's waht i want to achieve from the function template here.
I don't want to specify the type in fucntion as below,otherwise i have to write 4 different structures of the same type( for float,complex etc)
struct example
{
int a
float b
test<int> (*fun_ptr)(const test &a);
}
Hi Guys i think nobody is getting my point clearly..here i am explaining it at length
Firstly there is one .h file which consists of a template class,all the definitions of functions are defined in this class itself because its a template class.
//test.h
template<Data_T>
class test
{
test test_func_1(const test &var);
.....
};
There is another header file which has a structure "example" which should have a fucntion pointer as mentioned below,As "test" class is a template class i want this function pointer "fun_ptr" to behave like that
//test2.h
i can declare this structure like this (only one version,int only)
1. struct example
{
int a
float b
test<int> (*fun_ptr)(const test &a);
}
OR
2. template<typename Data_T>
struct example
{
int a
float b
test<Data_T> (*fun_ptr)(const test &a);
}
both of these version compiles successfully.
Now i want to declare an array of this templated structure e.g.
example example_array[]
{
{1,2,test_func_1}, //test_func_1 is defined in test.h file
{3,4,test_func_2}, //test_func_2 is defined in test.h file
};
But compiler gives an error at this point. If i use "template<typename Data_T>" before "example_array[]" then also compiler gives error.
Please tell me how to achieve this. Because of templates it is giving me errors.
Hi Galik,
Thanks for the detailed reply. But isn't the "example<int> example_array[]" restricting it to be only of type "int". What if i need to define for complex and doucble also? Then do i need to define them separately?
You have to declare the type of the array. There is no getting around that. C++ conspires to make you use the correct type from compile time. So you would need a different array for each type.
Because of "test<Data_T> (test<Data_T>::*fun_ptr)(const test<Data_T> &a)" be a templated function i had to declare the "struct example" as template structure. That's why i have to give type while declaring that array.
Is there any other way if i want to keep my "fun_ptr" as templated but the structure holding it to be a normal structure(non-template)?