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. 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.
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(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
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);
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(...
|
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...
...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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
|
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...
|
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...
|
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...
|
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...
|
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...
|
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,...
| |