Hi,

How do I use the generator in the file? I keep randomc.h as a header file in the Visual c++ project. My project has several header files and several .cpp files, many of which require to use the random no. generator. I have copied all the files in the zip folder into the folder for my project files.
Do I write this in each file that uses the generator?

#ifndef MULTIFILE_PROJECT
// If compiled as a single file then include these cpp files, 
// If compiled as a project then compile and link in these cpp files.
   // Include code for the chosen random number generator:
   #include "mersenne.cpp"
   // define system specific user interface:
   #include "userintf.cpp"
#endif


#include <time.h>                      // define time()
#include "randomc.h"

The compling is ok. But I get a lot of linking errors, e.g.,

GABaseSolver.obj : error LNK2005: "private: void __thiscall CRandomMersenne::Init0(int)" (?Init0@CRandomMersenne@@AAEXH@Z) already defined in GA1DArraySolution.obj

Please help.

in every header file it is a good idea to use inclusion guards. so to stop it from getting defined all the time just put

#ifndef RANDOMC_H
#define RANDOMC_H
//  all the code in randomc.h goes here
#endif

this will make sure that the file is only included the first time and every other time #include "randomc.h" is encountered the compiler will skip that file because it is already defined. I've found this makes things easier down the road and saves some debugging time.

Some compilers that are stupid the gaurds still wont work, if that is so try using pragma once at the top.

#pragma once
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.