Thread: Array of templated structure

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    5

    Exclamation Array of templated structure

    I have problem defining array of a templated structure

    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.
    Code:
    //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

    Code:
    //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.
    Code:
    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.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    My guess is that fun_ptr is supposed to be a member function pointer instead. I got it to work with:
    Code:
    template<typename Data_T>
    class test
    {
    public:
        test test_func_1(const test &var)
        {
            return var;
        }
    
        test test_func_2(const test &var)
        {
            return var;
        }
    };
    
    template<typename Data_T>
    struct example
    {
        example(int a, float b, test<Data_T> (test<Data_T>::*fun_ptr)(const test<Data_T> &a))
            : a(a), b(b), fun_ptr(fun_ptr) {}
    
        int a;
        float b;
        test<Data_T> (test<Data_T>::*fun_ptr)(const test<Data_T> &a);
    };
    
    int main()
    {
        example<int> example_array[] =
        {
            example<int>(1, 2, &test<int>::test_func_1),
            example<int>(3, 4, &test<int>::test_func_2),
        };
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jul 2010
    Posts
    5
    Yeah,your guess is right. It is a member function. But instead of declaring the "example_array[]" in main i want this array to be statically initialized. Because filling this array in main loop restrict it to the <int> as in example given by you.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by cplusplustudent
    But instead of declaring the "example_array[]" in main i want this array to be statically initialized. Because filling this array in main loop restrict it to the <int> as in example given by you.
    What do you mean? It will still be an array of example<int> or whatever other type you want whether it has static storage duration or not.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jul 2010
    Posts
    5
    I got your point. I can not declare it without giving any type as compiler needs to reserve the memory for array and it needs to know for which type. This is my understanding.

  6. #6
    Registered User
    Join Date
    Jul 2010
    Posts
    5
    I am not able to compile the code. As it says that "incomplete type ... used in nested name specifier". I suppose compiler is complaining because it needs the full definition of "test" class.
    But test is present in test.h file,which is a template file(having all the definition of templated member functions of test class), example is in different file test2.h.
    I can not include test.h in test2.h as compiler is again complaining.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    This does not give us a lot of information as to what is wrong. I suggest you post an example.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Jul 2010
    Posts
    5
    I created a small project and listing out the files below

    //test.h
    Code:
    #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
    Code:
    #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
    Code:
    #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
    Code:
    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;
    }

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You should include test.h. You have a simple typo:
    Code:
    test<Data_T> test<Data_T>::test_func_1(const Data_T &var)
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem Passing Structure Reference To Function
    By soj0mq3 in forum C Programming
    Replies: 9
    Last Post: 04-24-2010, 10:27 AM
  2. dynamic array and structure problem
    By bluetxxth in forum C Programming
    Replies: 3
    Last Post: 04-13-2010, 06:56 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 01:01 AM