LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   template problem in c++ (https://www.linuxquestions.org/questions/programming-9/template-problem-in-c-831747/)

101b147 09-12-2010 03:20 PM

template problem in c++
 
hey guys. i'm having some problems in using templates in c++. here is the code i'm trying to compile. i'll send a shrinked version.

Code:

#include<list>
#include<utility>
#include<algorithm>
#include<string>
#include<vector>
using namespace std;

template<class K, class V> struct Hash{
        list<pair<K, V> > *hash;       
        int (*hashFunction)(K key);       
        int buckets;                       

        Hash(int (*hashFunction)(K), int buckets){};

        ~Hash(){};

        list<pair<K, V> >::iterator put(K key, V value, int pos_flag = -1){ return 0; };
};

int main(){
        return 0;
}

the problem is that i don't know how to specify the return type of the put() method.
when i change the return type to
Code:

list<pair<int, int> >::iterator
, it compiles normally.

thx in advance.

johnsfine 09-12-2010 06:12 PM

The problem is that the compiler can't know that
list<pair<K, V> >::iterator
is the name of a type. Even though the compiler has at that point seen the whole definition of list so you would expect it to know iterator is a name of a type inside list, the C++ standard has rules that prevent it from knowing.

In this situation, I strongly prefer to use several typedefs to break up the ugly declarations. So you could fix your problem just by adding the keyword typename to your declaration. But I personally think it is very ugly to use typename that way anywhere other than right after the keyword typedef

I haven't tested any of this, but I think it is right:

Code:

#include<list>
#include<utility>
#include<algorithm>
#include<string>
#include<vector>
using namespace std;

template<class K, class V> struct Hash{
        typedef list<pair<K, V> > my_list;
        typedef typename my_list::iterator iterator;
        my_list *hash;       
        int (*hashFunction)(K key);       
        int buckets;                       

        Hash(int (*hashFunction)(K), int buckets){};

        ~Hash(){};

        iterator put(K key, V value, int pos_flag = -1){ return 0; };
};

Actually I prefer the following, but that makes changes further from the scope of your question
Code:

#include<list>
#include<utility>
#include<algorithm>
#include<string>
#include<vector>

template<class K, class V> struct Hash{
        typedef std::pair<K, V> pair;
        typedef std::list<pair> list;
        typedef typename list::iterator iterator;
        list *hash;       
        int (*hashFunction)(K key);       
        int buckets;                       

        Hash(int (*hashFunction)(K), int buckets){};

        ~Hash(){};

        iterator put(K key, V value, int pos_flag = -1){ return 0; };
};


101b147 09-13-2010 12:17 PM

thx, that solved my problem!! and thx for the extra explanation.


All times are GMT -5. The time now is 02:47 AM.