can not create a function pointer of class

Pages: 123
I tried the first approach suggested by you Galik. But it is giving error at line number 3 saying "incomplete type ... used in nested name specifier"
1
2
3
4
example<int> example_array[] =
{
	{1, 2, &test<int>::test_func_1}
};
I need a static table and based upon the match find in the table i want to execute that corresponding function in the table entry. And those functions i want to keep templated as to handle various data types.
I don't know whether I have made myself clear or not in my last post.
Actually i want to create a static table either map/vector/list/array of type "example". So that in later part of my code i can navigate through its entries.
Incomplete type means that the compiler needs to see the full declaration of the class/struct involved. In this case probably it needs to see the whole of template class test.
To what purpose is this navigating through the array? Is it some kind of game controller?
But that code is in test.h file which is a template class code. I can not include it in test2.h. I can only give forward declaration of test class.
It's not a game controller.It's a kind of library to perform various operation on various objects depending upon their type.
Sounds like you want polymorphism, as was stated a while back.
Galik,i am not able to compile as mentioned in my last post. And suggested by you that I need to use the whole template class,that also i can not do because i can not include the test.h header in test2.h as test.h header has all teh definitions of the functions in the same file. So compiler gives error to that also.
I don't understand what's going wrong, can you post both headers and source files?
Hi Galik,actual files are quite big i can not post them here. But i did precisely as u did. But still error persists.
Hi Galik, i created a small project and listing out the files below

//test.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>


template<typename Data_T>
class test
{
public:
	test test_func_1(const Data_T &var);
};

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


//test2.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <test.h>


template<typename Data_T> class test;



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


example<int> example_array[] =
{
	{1, 2, &test<int>::test_func_1},
	{1, 2, &test<int>::test_func_1},
};


//main.cpp
1
2
3
4
5
6
7
8
#include <iostream>
#include <test2.h>

int main()
{
	std::cout<<"test_func"<<example_array[0].a<<std::endl;
	
}



I can not include test.h file in test2.h file because if i do so then it gives error at
1
2
3
4
5
template<typename Data_T >
test test<Data_T>::test_func_1(const Data_T &var) //error: expected constructor, destructor, or type conversion before ‘test’
{
		std::cout << "test_func_1: " << var << std::endl;
}
Last edited on
It looks like you forgot to supply your template parameter:
1
2
3
4
5
template<typename Data_T >
test<Data_T> test<Data_T>::test_func_1(const Data_T &var)
{
		std::cout << "test_func_1: " << var << std::endl;
}
Last edited on
Got it Galik.its working now. I have one more doubt. what if test.h is included in test2.h and test2.h is included in test.h?
Should that cause problem because i am getting issue doing so. And i have to to do so because of dependencies of each file on other.
You should put your header files in include guards:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#ifndef _TEST_H_
#define _TEST_H_

#include <iostream>

template<typename Data_T>
class test
{
public:
	test test_func_1(const Data_T &var);
};

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

#endif // _TEST_H 


http://en.wikipedia.org/wiki/Include_guard

Thanks for the reply Galik,i have already done the include guards.
Could you please tell me how to define the vector of the "example".
I am declaring it as below because example is a templated structure. But compiler is giving error :(
1
2
template<typename Data_T >
std::vector<example<Data_T> > example_vect;
You don't declare define it using the template keyword. You simply supply the template parameters like this:

1
2
std::vector<example<int> > example_int_vect;
std::vector<example<double> > example_double_vect;
Last edited on
So in this way Galik we can not define vector of a template class?
std::vector is already a template class. You can't redefine it. All you can do is define instances of it by supplying the template parameters.

Got your point Galik.
Pages: 123