0

I'm new to C++. I want to use the header file Random.h (described here http://ftp.arl.mil/random/). It uses unistd.h Which gives me an error

"Cannot open include file: 'unistd.h': No such file or directory"

I've googled around and seen that the issue is that unistd.h works with unix not visual express. Some posts suggest using mingw instead but I've tried that and it doesn't work. Can anyone tell me how to get unistd.h (or at least the bits of it I need) to work on Visual Express. There are some suggestions online but I cant find anything that addresses my particular question. thanks!

user3111174
  • 121
  • 1
  • 1
  • 5
  • 2
    That's an old library. C++ today has a standard library containing `` (note: no .h). It was added to VC++ back in 2008. – MSalters Dec 17 '13 at 13:20
  • but random doesn't let you define your own distributions (Random.h does) – user3111174 Dec 17 '13 at 13:32
  • PS I had already found the "answer" you point out. But this did not address my particular problem (this is exactly what I was referring to in the last line of my original post) – user3111174 Dec 17 '13 at 14:13
  • 1
    @user3111174 yes it does – jalf Dec 17 '13 at 14:21
  • jalf- you are too quick to assume what is and isn't helpful. I studied that previous answer in great detail and implemented it. Didnt work for me. The detailed answer below (which was SPECIFIC to my question) did solve my problem. The previous answer was not clear enough for someone new to C++. You may think they are the same but for me it was the difference between my code working and it not... – user3111174 Dec 18 '13 at 10:41

2 Answers2

0

It is including unistd.h for calls to getpid(). Change the #include to <process.h> and change all the getpid() calls to _getpid() (note the extra underscore at the front).

msdn.microsoft.com/en-us/library/t2y34y40.aspx

Alan
  • 1,895
  • 1
  • 10
  • 9
  • thanks Alan. By "Change the #include to" do you mean delete the line #include or were you saying I should change it to another header file (I think the end of your sentence might be missing) – user3111174 Dec 17 '13 at 12:38
  • thanks Alan. By "Change the #include to" do you mean delete the line #include or were you saying I should change it to another header file (I think the end of your sentence might be missing) – user3111174 Dec 17 '13 at 12:40
  • I have fixed the answer. The angle brackets were being treated as HTML – Alan Dec 17 '13 at 12:41
  • I've tried #include and replacing getpid() with _getpid() but I still get loads of errors: error C2864: 'Random::_F' : a static data member with an in-class initializer must have non-volatile const integral type error C2065: 'M_PI' : undeclared identifier etc – user3111174 Dec 17 '13 at 12:45
  • Did you put #define _USE_MATH_DEFINES before #include ? Have another look at that link. – Alan Dec 17 '13 at 12:56
  • great that helps. Still have the other error though: error C2864 : 'Random::_F' : a static data member with an in - class initializer must have non - volatile const integral type – user3111174 Dec 17 '13 at 12:59
0

Visual C++ will not let you assign static const doubles in the header file (ony integer types). You will need to change the line in the .h file to this...

static const double _F;

Then create a Random.cpp file that just contains this...

#include "Random.h"

// Initialize static variable
const double Random::_F    = 1. / Random::_M;
Alan
  • 1,895
  • 1
  • 10
  • 9