473,395 Members | 1,568 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

confusion about header and implementation files

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.
Jul 23 '05 #1
8 1907
Roman Töngi wrote:
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

--
Anti-spam address, change each 'X' to '.' to reply directly.
Jul 23 '05 #2
On Fri, 06 May 2005 22:42:12 +0200, Roman Töngi wrote:

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

Jul 23 '05 #3
Do I separate the header and implementation files right
in this example? Is it necessary to include random.h in random.cpp?

Thanks
Jul 23 '05 #4
On Fri, 06 May 2005 23:53:03 +0200, Roman Töngi wrote:
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

Jul 23 '05 #5
On Fri, 06 May 2005 21:06:47 GMT, Larry I Smith
<la***********@verizon.net> wrote in comp.lang.c++:
Roman Töngi wrote:
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


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.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jul 23 '05 #6
Jack Klein wrote:
On Fri, 06 May 2005 21:06:47 GMT, Larry I Smith
<la***********@verizon.net> wrote in comp.lang.c++:
Roman Töngi wrote:
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


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);
Jul 23 '05 #7
Larry I Smith wrote:
Jack Klein wrote:
On Fri, 06 May 2005 21:06:47 GMT, Larry I Smith
<la***********@verizon.net> wrote in comp.lang.c++:
[...]
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
Jul 23 '05 #8
Victor Bazarov wrote:
Larry I Smith wrote:
Jack Klein wrote:
On Fri, 06 May 2005 21:06:47 GMT, Larry I Smith
<la***********@verizon.net> wrote in comp.lang.c++:

>[...]
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

--
Anti-spam address, change each 'X' to '.' to reply directly.
Jul 23 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: dharmesh Gupta | last post by:
Hi All, i am new to header files , and have a basic question , i have made my program in three files namely, idr_bpl.cpp( contains class definitions and function definitions) bpl.cpp(...
3
by: Chris Mantoulidis | last post by:
Seperate compilation (that's what it's called, right?) seems to be quite popular, so I decided to get some info about it, and (d'oh) use it... But it's whole structure seems weird to me... ...
16
by: matthurne | last post by:
I just started learning C++ on my own...I'm using Accelerated C++. Something it hasn't explained and I keep wondering about is how header files actually work. I suspect it doesn't get into it...
11
by: Steven T. Hatton | last post by:
In the past there have been lengthy discussiions regarding the role of header files in C++. People have been very adamat about header files serving as in interface to the implementation. I do...
2
by: puzzlecracker | last post by:
after reading some of the post I found out something rather radical to my previous understanding: that when you do #include<iostream> #include<string> #include<vector> etc compiler puts...
3
by: pooja | last post by:
Suppose i have created a class c1 with f1()in c1.cpp and included this c1.cpp in file1.cpp file , which is also having main() by giving the statement #include "c1.cpp". the same i can do by...
60
by: Derrick Coetzee | last post by:
It seems like, in every C source file I've ever seen, there has been a very definite include order, as follows: - include system headers - include application headers - include the header...
10
by: joelagnel | last post by:
hi friends, i've been having this confusion for about a year, i want to know the exact difference between text and binary files. using the fwrite function in c, i wrote 2 bytes of integers in...
36
by: zouyongbin | last post by:
Stanley B Lippman in his "C++ Primer" that a definition like this should not appear in a header file: int ix; The inclusion of any of these definitions in two or more files of the same...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.