confusion about header and implementation files

R

Roman Töngi

I don't yet understand how to divide a C++ project
into header and implementation files. The following won't
compile:

// Apple.cpp
#include <iostream>
#include "random.h"
using namespace std;
int main()
{
cout << random();
return 0;
}

// random.h
double random();

// random.cpp
#include <cstdlib>
#include <ctime>
// is it necessary to include random.h?
#include "random.h"
double random()
{
double rnd;
std::srand(static_cast<unsigned>(std::time(NULL)));
rnd = static_cast<double>(std::rand()) / RAND_MAX;
return rnd;
}

There is an error message something like this: searching error for directive
for the precompiled header file.
 
L

Larry I Smith

Roman said:
I don't yet understand how to divide a C++ project
into header and implementation files. The following won't
compile:

// Apple.cpp
#include <iostream>
#include "random.h"
using namespace std;
int main()
{
cout << random();
return 0;
}

// random.h
double random();

// random.cpp
#include <cstdlib>
#include <ctime>
// is it necessary to include random.h?
#include "random.h"
double random()
{
double rnd;
std::srand(static_cast<unsigned>(std::time(NULL)));
rnd = static_cast<double>(std::rand()) / RAND_MAX;
return rnd;
}

There is an error message something like this: searching error for directive
for the precompiled header file.

The <cstdlib> (aka <stdlib.h>) already has a function
named random(). Your compiler is getting confused.
Rename your function (myRandom() perhaps) -or- put
your random() in a seperate namespace.

Larry
 
J

Jay Nabonne

There is an error message something like this: searching error for directive
for the precompiled header file.

Turn off precompiled headers in Visual Studio. Or include "stdafx.h" as
the first thing in each .cpp file.

- Jay
 
R

Roman Töngi

Do I separate the header and implementation files right
in this example? Is it necessary to include random.h in random.cpp?

Thanks
 
J

Jay Nabonne

Do I separate the header and implementation files right
in this example? Is it necessary to include random.h in random.cpp?

Thanks

You have the right idea. The only change I would do is make it:

extern double random();

in random.h. Doesn't matter much, but it shows the intent more.

And while not technically necessary to include random.h in random.cpp,
I think it's better; you can catch some kinds of prototype mismatches if
the cpp file sees the prototype for the function (e.g. if you
accidentally change the return type in one place but not the other).

- Jay
 
J

Jack Klein

The <cstdlib> (aka <stdlib.h>) already has a function
named random(). Your compiler is getting confused.
Rename your function (myRandom() perhaps) -or- put
your random() in a seperate namespace.

Larry

No, it does not. Not if it is a standard conforming C++ compiler.
There is no function named "random" in the standard C++ (or C)
library, and that name is in the application's namespace so it is
invalid for an implementation-specific extension.
 
L

Larry I Smith

Jack said:
No, it does not. Not if it is a standard conforming C++ compiler.
There is no function named "random" in the standard C++ (or C)
library, and that name is in the application's namespace so it is
invalid for an implementation-specific extension.

From 'man -S 3 random':

<quote>

NAME
random, srandom, initstate, setstate - random number generator

SYNOPSIS
#include <stdlib.h>

long int random(void);
 
V

Victor Bazarov

Larry said:
Jack said:
[...]
The <cstdlib> (aka <stdlib.h>) already has a function
named random(). Your compiler is getting confused.
Rename your function (myRandom() perhaps) -or- put
your random() in a seperate namespace.

Larry

No, it does not. Not if it is a standard conforming C++ compiler.
There is no function named "random" in the standard C++ (or C)
library, and that name is in the application's namespace so it is
invalid for an implementation-specific extension.

[..]
Conforming, or not, there may be a conflicting 'random()'
in his <stdlib.h>.

May be or may NOT be. Tha's the whole point Jack is making: you
said "The <cstdlib> .. already has a function named random". The
reality is that it does on *your* platform. How you arrive to the
conclusion that it causes conflict on the OP's platform is beyond
us. You must know something we don't.

V
 
L

Larry I Smith

Victor said:
Larry said:
Jack said:
On Fri, 06 May 2005 21:06:47 GMT, Larry I Smith

[...]
The <cstdlib> (aka <stdlib.h>) already has a function
named random(). Your compiler is getting confused.
Rename your function (myRandom() perhaps) -or- put
your random() in a seperate namespace.

Larry
No, it does not. Not if it is a standard conforming C++ compiler.
There is no function named "random" in the standard C++ (or C)
library, and that name is in the application's namespace so it is
invalid for an implementation-specific extension.
[..]
Conforming, or not, there may be a conflicting 'random()'
in his <stdlib.h>.

May be or may NOT be. Tha's the whole point Jack is making: you
said "The <cstdlib> .. already has a function named random". The
reality is that it does on *your* platform. How you arrive to the
conclusion that it causes conflict on the OP's platform is beyond
us. You must know something we don't.

V

Yeah, I tend to forget that GCC on unix/linux is not the only
package folks use. MS Windows confuses me :)

Sorry.

Regards,
Larry
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top