A little help please...

Hello,

I am trying to write my own random number generator using OOP techniques. However, I am getting an error that I am unfamiliar with and was wondering if anyone could help me out. Below is my code for an RNG (random number generator).

//RNG.h
//Random Number Generator class definitions
#include <ctime>
#include <cstdlib>
#include <iostream>
using namespace std;

class RNG
{
public:
RNG();//default constructor
void setRNG(double); //set RNG number
double getRNG(); //return RNG

private:
double randomNumber; //store random number
}; //end class definitions



//RNG.cpp
//Random Number Generator member-function definitions
#include <iostream>
#include <ctime>
#include <cstdlib>
#include "RNG.h"
using namespace std;


RNG::RNG()
{
srand(time(NULL));
}

void RNG::setRNG(double random)
{
randomNumber = rand() % 6 + 1;
}

double RNG::getRNG()
{
return randomNumber;
}



//Driver.cpp
//Testing program

#include "RNG.h"
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;

int main()
{
srand (time(NULL));
RNG rando();

cout<< "Random 6 sided die roll is: "<<endl;

cout<<rando().getRNG()<<endl;
}//end main



Thank you for your help in advance.

~Soundstone
Oh, BTW...

the error is:


c:\users\matt\desktop\programming projects\gamepractice\gamepractice\rng.cpp(12): warning C4244: 'argument' : conversion from 'time_t' to 'unsigned int', possible loss of data
1>Driver.obj : error LNK2019: unresolved external symbol "class RNG __cdecl rando(void)" (?rando@@YA?AVRNG@@XZ) referenced in function _main
1>C:\Users\Matt\Desktop\programming projects\GamePractice\Debug\GamePractice.exe : fatal error LNK1120: 1 unresolved externals


Thanks again for the help

~Soundstone
You don't need the () after rando.

1
2
3
4
5
6
7
8
9
int main()
{
srand (time(NULL));
RNG rando; // HERE

cout<< "Random 6 sided die roll is: "<<endl;

cout<<rando.getRNG()<<endl; // HERE
}//end main 


(With the (), the compiler thinks you're providing a forward definition to a function called rando which returns a RNG value. You do need the (), of course, if your constructor takes a parameter)

And to shut up the warning add the following #define before you includes

#define _USE_32BIT_TIME_T

The default is to use a 64-bit version of time_t; this define get the code to use the old 32-bit version, which is the same size as an unsigned int.

Or you could use a cast, as you are just feeding the value to srand.

Andy

P.S. If you want to make you code (and output) look prettier, see "How to use tags"
http://www.cplusplus.com/articles/z13hAqkS/
Last edited on
Thank you for you help Andy. I really appreciate it!

Regards,

~Soundstone
Topic archived. No new replies allowed.