LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 09-12-2010, 04:20 PM   #1
101b147
LQ Newbie
 
Registered: Jan 2009
Distribution: Fedora
Posts: 24

Rep: Reputation: 15
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.
 
Old 09-12-2010, 07:12 PM   #2
johnsfine
Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,125

Rep: Reputation: 1119Reputation: 1119Reputation: 1119Reputation: 1119Reputation: 1119Reputation: 1119Reputation: 1119Reputation: 1119Reputation: 1119
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; };
};

Last edited by johnsfine; 09-12-2010 at 07:24 PM.
 
1 members found this post helpful.
Old 09-13-2010, 01:17 PM   #3
101b147
LQ Newbie
 
Registered: Jan 2009
Distribution: Fedora
Posts: 24

Original Poster
Rep: Reputation: 15
thx, that solved my problem!! and thx for the extra explanation.
 
  


Reply

Tags
template


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Compilation problem in c++ template code :( lali.p Programming 2 10-13-2008 07:44 PM
from django.template import Template, gives output firedancer Linux - Newbie 0 11-30-2007 03:08 PM
Template class with a template member... Nicholas Bishop Programming 3 02-21-2005 09:27 PM
Template related problem in Linux ashwinipahuja Programming 2 04-23-2004 01:10 AM
problem in template function. compiles in vc++ not in gcc 3.0.4 cybercop12us Programming 4 12-12-2002 02:04 AM


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

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration