F
Fred H
I'm currently trying to write a function template that
can fill a variable of arbitrary type with 'random' stuff,
but I can't seem to get my function template working.
In my .h file I've declared the function template like this,
inside a class I'm building:
template <typename T> T RandBits();
In my .cpp file I've implemented it like this:
/* Function: RandBits */
template <typename T> T hdd::HexGen::RandBits() {
T bits = T();
for(int i = 0; i < sizeof(T); i++) {
bits <<= 8; //Left shift by 8
bits |= rand() % 0xFF; //Insert 8 'random' bits
//printf("%08X\n",bits); //Shows the progression
}
return bits;
}//EndOf RandBits()
Even though the FAQ says I should use 'extern', I've omitted
it, because I get an error message using it.
I don't get any linking errors, but as suggested by the FAQ,
I've included the following in the same .cpp file:
template hdd::HexGen::RandBits<int>();
Now, in another .cpp file, where I've got my main, I
instansiate an object of my class, and call the template
function like this:
long lngRand = hg.RandBits<long>();
cout << "\n" << hex << lngRand << endl;
'hg' is of course a object of the mentioned class.
Using VS VC 6.0 SP5(?), I get the following error:
c:\programfiler\microsoft visual
studio\myprojects\tutorialtests\myxor\main.cpp(12) : error C2062: type
'long' unexpected
Why is 'long' unexpected? Can anyone see what I'm doing wrong? I suppose
I've stared myself blind on this problem by now, so all inputs are
appreaciated
--
Fred H
void FredH::Contact() {
TextToSpeach.say("frode at age dee dee dot en oh");
}
can fill a variable of arbitrary type with 'random' stuff,
but I can't seem to get my function template working.
In my .h file I've declared the function template like this,
inside a class I'm building:
template <typename T> T RandBits();
In my .cpp file I've implemented it like this:
/* Function: RandBits */
template <typename T> T hdd::HexGen::RandBits() {
T bits = T();
for(int i = 0; i < sizeof(T); i++) {
bits <<= 8; //Left shift by 8
bits |= rand() % 0xFF; //Insert 8 'random' bits
//printf("%08X\n",bits); //Shows the progression
}
return bits;
}//EndOf RandBits()
Even though the FAQ says I should use 'extern', I've omitted
it, because I get an error message using it.
I don't get any linking errors, but as suggested by the FAQ,
I've included the following in the same .cpp file:
template hdd::HexGen::RandBits<int>();
Now, in another .cpp file, where I've got my main, I
instansiate an object of my class, and call the template
function like this:
long lngRand = hg.RandBits<long>();
cout << "\n" << hex << lngRand << endl;
'hg' is of course a object of the mentioned class.
Using VS VC 6.0 SP5(?), I get the following error:
c:\programfiler\microsoft visual
studio\myprojects\tutorialtests\myxor\main.cpp(12) : error C2062: type
'long' unexpected
Why is 'long' unexpected? Can anyone see what I'm doing wrong? I suppose
I've stared myself blind on this problem by now, so all inputs are
appreaciated
--
Fred H
void FredH::Contact() {
TextToSpeach.say("frode at age dee dee dot en oh");
}