I actually got your first source code post to work with a few modifications. First was to add a constructor call to rng.cpp ( aRandomNumberGenerator::aRandomNumberGenerator() ), second was to remove the include of rng.h in main.cpp and I also added an endl to your print out of the random numbers for readability...the following code compiled for me using the g++ compiler on a CentOS 5.4 machine.
rng.h
Code:
#ifndef RNG_H
#define RHG_H
//aRandomNumberGenerator class definition
class aRandomNumberGenerator {
public:
//Constructor initializes aRandomNumberGenerator
aRandomNumberGenerator();
void setSeed(unsigned long);
double generate();
private:
unsigned long seed;
}; //end of class aRandomNumberGenerator
#endif
rng.cpp
Code:
#include <iostream>
#include <cstdlib>
#include "rng.h"
aRandomNumberGenerator::aRandomNumberGenerator() {
}
void aRandomNumberGenerator::setSeed(unsigned long)
{
seed = 2345;
srand(seed);
}
double aRandomNumberGenerator::generate()
{
return rand();
}
main.cpp
Code:
#include <iostream>
#include <cstdlib>
#include "rng.cpp"
using namespace std;
int main(void) {
aRandomNumberGenerator myrng;
cout << "Ten random numbers are: " << endl;
for(double i = 0; i<=10; i++) {
cout << myrng.generate() << endl;
}
return 0;
}