473,765 Members | 1,967 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(stat ic_cast<unsigne d>(std::time(NU LL)));
rnd = static_cast<dou ble>(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 1930
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(stat ic_cast<unsigne d>(std::time(NU LL)));
rnd = static_cast<dou ble>(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(stat ic_cast<unsigne d>(std::time(NU LL)));
rnd = static_cast<dou ble>(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.l earn.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(stat ic_cast<unsigne d>(std::time(NU LL)));
rnd = static_cast<dou ble>(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
5133
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( contains main() function) bpl.h (contains variable definition of a variable that is shared in the above two files )
3
2228
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... Here's what I think of how it is (from what I've read): THE PROJECT +1st header file
16
12547
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 because it is, as the authors love to say, "implementation specific". If that's the case, how does the compiler commonly handle them? I use Linux and gcc specifically. Basically, I don't understand how a header file being included makes a...
11
2762
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 understand the objective. This example may represent an interface in need of a bit of refactoring, but it goes to demonstrate the basic idea as I understand it. http://developer.kde.org/documentation/library/cvs-api/kdevelop/html/ast_8h-source.html...
2
5618
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 those .h files into the namespace std -.h files that contain all the declarations for vector, string, etc...
3
3096
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 using header file. i can create a class c1 with f1() in c1.h and include this c1.h in file1.cpp by giving the statement #include "c1.h" tell me that what exactly is the difference between c1.h and c1.cpp? Since they both are doing the same things.
60
8316
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 associated with this source file For example, in a file hello.c: #include <stdio.h>
10
3664
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 binary mode. according to me, notepad opens files and each byte of the file read, it converts that byte from ascii to its correct character and displays
36
3854
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 program will result in a linker error complaining about multiple definitions. So this kind of definition should be avoided as much as possible. But as we know, the definition of a class is always in a header file. And we can use "#ifndef" to eliminate...
0
9398
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10160
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10007
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9832
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8831
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5421
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3924
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3531
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.