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. 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.
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
Do I separate the header and implementation files right
in this example? Is it necessary to include random.h in random.cpp?
Thanks
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
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
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);
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
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 )
|
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
|
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...
|
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...
|
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...
| |
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.
|
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>
|
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
|
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...
|
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,...
|
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...
| |
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...
|
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...
|
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...
|
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();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |