can not create a function pointer of class

Pages: 123
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.

Its a template class so you need to specify what is substituted in for Data_T. When creating an object


ex:
1
2
3
4
5
6
7
struct example
{
int a
float b
test<int> (*fun_ptr)(const test &a);
}

Also

1
2
template<Data_T>
class test


should look like

1
2
3
4
5
6
7
template<typename Data_T>
class test

//or

template<class Data_T>
class test

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.
On what basis is the type of the template parameter going to be decided?
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 am still waiting for the solution guys :(
The problem still isn't very clear. Could you explain what you're actually trying to do (the big picture)?
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.
Templates allow compile-time polymorphism only.
Inheritance and virtual functions allow runtime polymorphism.
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.
Okay, I think your syntax for declaring member function pointers needs some work ;o)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
template<typename Data_T>
class test
{
public:
	test<Data_T> test_func_1(const test &var)
	{
		// ... stuff ...
	}
};

template<typename Data_T>
struct example
{
	int a;
	float b;
	test<Data_T> (test<Data_T>::*fun_ptr)(const test<Data_T> &a);
};

example<int> example_array[] =
{
	{1, 2, &test<int>::test_func_1}
};
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.
Or use boost::function<> to hold the pointers.


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)?
You could create a templatized array by placing it in a template class or a template function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <cstdlib>

template<typename Data_T>
class test
{
public:
	void test_func_1(const Data_T &var)
	{
		std::cout << "test_func_1: " << var << std::endl;
	}
};

template<typename Data_T>
struct example
{
	int a;
	float b;
	void (test<Data_T>::*fun_ptr)(const Data_T& a);
};

template<typename Data_T>
example<Data_T>& get_example(const Data_T&, size_t i)
{
	static example<Data_T> example_array[] =
	{
		{1, 2, &test<Data_T>::test_func_1}
	};
	return example_array[i];
}

int main()
{
	int i = 3;
	double d = 23.78;

	test<int> a;
	test<double> b;

	(a.*get_example(int(), 0).fun_ptr)(i);
	(b.*get_example(double(), 0).fun_ptr)(d);
}
test_func_1: 3
test_func_1: 23.78


But you still have to know the type before calling it.
cplusplusstudent wrote:
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)?

To be honest, when things get this convoluted, you might want to consider another approach to whatever problem you are trying to solve.
Last edited on
Okey,i'll try another approach,but template will still be there :(
What are you actually trying to achieve with all this?
Pages: 123