Problems with template functions and rand/srand

What is wrong with this code?

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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#ifndef TFE_STATISTICS_H
#define TFE_STATISTICS_H
#include <iostream>
#include <time.h>
class TFE_Statistics{
	protected:
		// Protected static constant stats
        static const bool TFE_EXTENSION = false, TFE_GENERIC = true;
		static const short TFE_VAL_DEFAULT=100, TFE_VAL_SMALL_MIN = -4096, TFE_VAL_SMALL_MAX = 4096;
		// Protected, generally used variables
        bool bThisExists, bThisExtendable;
        short sThisExtensionAmount, sThisExtensionAmountMax, sThisGenericAmount, sThisGenericAmountMax;
        // Protected functions
		template<typename T> inline bool CheckExistanceInRange(T,T,T) const;
        template<typename T> inline bool CheckType(bool,T) const;
		template<typename T> inline void GenerateUniqueRand(T*,int,T,T) const;
		template<typename T> inline void LimitIntoRange(bool&,T&,T,T) const;
		template<typename T> inline void LimitIntoRange(T&,T,T) const;
};

// -------------------
// Protected functions
// -------------------
template<typename T>
inline bool TFE_Statistics::CheckExistanceInRange(T variable, T range_start, T range_end) const{
	if(this->CheckType(true,variable)){
		if(variable >= range_start && variable <= range_end) return true;
		else return false;
	}
}
template<typename T>
inline bool TFE_Statistics::CheckType(bool is_number, T) const{
	if(is_number){
		if(sizeof(T) <= 2) return false;
		else return true;}
	else if(!is_number) return false;
	else return false;
}
template<typename T>
inline void TFE_Statistics::GenerateUniqueRand(T *storage_array, int elements, T range_start, T range_end) const{
	srand(time(0));
	for(int i=0; i<elements; i++){
		storage_array[i] = rand() % range_end;
		for(int j=0; j<i; j++){
			while(storage_array[i] < range_start || storage_array[i] == storage_array[j])
				storage_array[i] = rand() % range_end;
		}
	}
}
template<typename T>
inline void TFE_Statistics::LimitIntoRange(bool &state_of_this, T &variable, T range_start, T range_end) const{
	if(this->CheckType(true,variable)){
		if(variable > range_end){
			variable = range_end;
			state_of_this = true;}
		else if(variable <= range_start){
			variable = range_start;
			state_of_this = false;}
		if(variable > range_start)
			state_of_this = true;
	}
}
template<typename T>
inline void TFE_Statistics::LimitIntoRange(T &variable, T range_start, T range_end) const{
	if(this->CheckType(true,variable)){
		if(variable < range_start)
			variable = range_start;
		else if(variable > range_end)
			variable = range_end;
	}
}
#endif // TFE_STATISTICS_H 


Error(s): there are no arguments to 'rand/srand' that depend on a template parameter, so declaration of 'rand/srand' must be available.

I have never seen this happening for me. Then it says:

Note: if you use '-fpermissive', G++ will accept your code.

Template functions have something wrong, right?
Remember my comments about testing with type int in http://www.cplusplus.com/forum/general/42424/#msg229260 ?
Oops, I seem to have used older piece of code in my template function while I had a little copying. Thanks, kbw, I will remember your help!
Last edited on
Topic archived. No new replies allowed.