can someone explain why
int k; canNOT be definied in .h file?
what are general rules for what can/cannot be placed in .h file. FAQ
doesn't seem to address this problem. 10 1465
davidru...@warp mail.net wrote: See Lakos ;-)
I have been interviewed by him not so long ago.. i.e. forgot to ask
this question. but he is probably expensive for private consultations,
I would assume!
suggest something more within the scope of this group or ballpark me
through an explanation.
thanks
puzzlecracker wrote: can someone explain why
int k; canNOT be definied in .h file?
The purpose of header files is to be '#include'd into several
translation units. If you place a definition of an object with external
linkage in header file, you'll sooner or later end up with several
definitions of this object in the program. This is a violation of ODR
(One Definition Rule) normally resulting in compilation error (at
linking stage). That's why such definition cannot be placed into header
file.
what are general rules for what can/cannot be placed in .h file.
H-files are for non-defining declarations and for certain kinds of
definitions. Namely, class definitions, template definitions, inline
function definitions, typename definitions can/should be placed into
header files. Entities with static linkage can also be defined in header
files, if for some reason you need to do so. Everything else (unless I
missed something) should only be _declared_ (as opposed to _defined_) in
header files.
--
Best regards,
Andrey Tarasevich
Andrey Tarasevich wrote: puzzlecracker wrote: can someone explain why
int k; canNOT be definied in .h file? The purpose of header files is to be '#include'd into several translation units. If you place a definition of an object with
external linkage in header file, you'll sooner or later end up with several definitions of this object in the program. This is a violation of ODR (One Definition Rule) normally resulting in compilation error (at linking stage). That's why such definition cannot be placed into
header file.
what are general rules for what can/cannot be placed in .h file. H-files are for non-defining declarations and for certain kinds of definitions. Namely, class definitions, template definitions, inline function definitions, typename definitions can/should be placed into header files. Entities with static linkage can also be defined in
header files, if for some reason you need to do so. Everything else (unless
I missed something) should only be _declared_ (as opposed to _defined_)
in header files.
-- Best regards, Andrey Tarasevich
3 questions in this regards:
a. if I define an object in .h but it is [.h file] only included in
just one file with no name conflicts, then it won't cause problems with
external linkage, but compiler still complains, why?
b. Why is inline function bypassing it? Actually b/c it is static in a
nature, but it is not discouraged as other static functions, objects,
etc., for bloating the space... why? also, sometimes, compiler chooses
not to inline, would it then affects the linkage?
c. what is the deal with 'const' definition in .h files... what is the
linkage rules applied to them?
THANKS THANKS THANKS
puzzlecracker wrote: can someone explain why
int k; canNOT be definied in .h file?
It can.
what are general rules for what can/cannot be placed in .h file. FAQ doesn't seem to address this problem.
Because it's not a problem. There are no rules.
You can write all you program in a .h file and then include it in a
..cpp file and include your .cpp file in .zzz file and compile it.
Everything's allowed. But it would be ridiculous.
P.Krumins
Peteris Krumins wrote: puzzlecracker wrote: can someone explain why
int k; canNOT be definied in .h file?
It can.
what are general rules for what can/cannot be placed in .h file.
FAQ doesn't seem to address this problem.
Because it's not a problem. There are no rules. You can write all you program in a .h file and then include it in a .cpp file and include your .cpp file in .zzz file and compile it. Everything's allowed. But it would be ridiculous.
P.Krumins
I know it is! even confirmed it. I am curious what is the
justification for such standard.
thanks
puzzlecracker wrote: 3 questions in this regards:
a. if I define an object in .h but it is [.h file] only included in just one file with no name conflicts, then it won't cause problems with external linkage, but compiler still complains, why?
No, it shouldn't cause problems. What kind of complaint are you
receiving from the compiler?
b. Why is inline function bypassing it?
Because that's the way it is in C++.
Actually b/c it is static in a nature,
No, it is not static. Inline functions have external linkage by default.
Even though each translation unit gets its own definition of an inline
function, the address of the function should be the same in all
translation units.
but it is not discouraged as other static functions, objects, etc., for bloating the space... why?
I don't exactly understand this question.
also, sometimes, compiler chooses not to inline, would it then affects the linkage?
No. "Linkage" in C++ is a language level concept. It is not affected by
low-level compiler decisions.
c. what is the deal with 'const' definition in .h files... what is the linkage rules applied to them?
In C++ definitions of const-qualified objects have static linkage by
default. The rest follows.
--
Best regards,
Andrey Tarasevich
puzzlecracker wrote: Can someone explain why
int k;
cannot be defined in a *.h [header] file?
It certainly can.
what are general rules for what can/cannot be placed in a *.h [header] file.
Header files are simply files that are included
near the top [head] of a *.cc [source] file or another header file.
The *.h extension is merely a convention.
Files with the *.h extension or any other extension
including no extension may be included anywhere if a fail
but probably should *not* be called header files
unless they are included somewhere near the head of the file.
There are no rules for what an/cannot be placed in a header file
except when the header file is used to define a *module interface*
which is the most common use for a header file.
Like all other interfaces, a module interface has *no* substance.
It should not, by itself, cause the compiler to emit code
or reserve memory for data. It should not, for example,
contain definitions like:
int k;
which would compel the compiler to reserve memory for k.
It may contain type definitions and declarations like:
extern int k;
void f(void);
inline
int g(int i) {
return i + 1;
}
class X {
private:
int I;
public:
X(int i = 0): I(i) { }
};
etc.
A module interface (header file) should be idempotent.
The contents should be enclosed in "guard macros:
cat file.h
#ifndef GUARD_FILE_H
#define GUARD_FILE_H 1
// contents
#endif//GUARD_FILE_H
The module definition (header file) should be self-sufficient.
It should include every module interface (header file)
required to process it's contents.
[The C++] FAQ doesn't seem to address this problem.
The problem is that neither C or C++ possess a strong notion of modules
much less module interfaces like modula. See Modula-2 http://en.wikipedia.org/wiki/Modula-2
C and C++ programmers are obliged
to manufacture ad hoc module interfaces
and the most convenient way to do that is using header files. No, it is not static. Inline functions have external linkage by
default. Even though each translation unit gets its own definition of an
inline function, the address of the function should be the same in all translation units.
wait, if inline function has external linkage, wouldn't that cause a
link problem when units get assambled? or it is an exception to the
general rule of external linkage? This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: John Caruthers |
last post by:
HELP!
This problem is driving me crazy. All other web sites
that are being served out through this server are working,
including sites and virtual directories that are under the
Default Web Site, yet when I attempt to browse the Default
Web Site I get this error:
The page cannot be displayed
|
by: Rob Ratcliff |
last post by:
I'm compiling the latest version of a CORBA ORB called MICO on a Cray
X1. It makes heavy use of templates and namespaces.
Up until the link step, the C++ source code compiled flawlessly. But,
when it tried to link, I got the attached warnings and then an error.
Any ideas why the linker wouldn't see the objects in the library? They
look like pretty long names,
so maybe there is some type of symbol length or mangling issue going
on? The...
|
by: Daniel Moree |
last post by:
I'm using MS VC++ 6.0
I'm working on a big project. I've currently have several files for this
project.
Here's the problem.
I have one header file phead.h
I have two code files main.cpp and gameloop.cpp
phead.h has all my core declarations in it like my main globals.
main.cpp has all my window initilization functions and my winproc loop.
|
by: Erik |
last post by:
Hi Everyone,
I'm having real problems compiling some source for eVC4++. The errors
I am getting are below:
It all seems to be centred around winsock. If I move the afsock.h
reference to before my other includes then I get lots of errors like
C2011: 'fd_set' : 'struct' type redefinition
warning C4005: 'FD_CLR' : macro redefinition
which I understand are due to the fact that windows.h is being
included in another header file as well as...
|
by: matvdl |
last post by:
I have a system that was originally developed in asp - the pages are saved in
SQL (there are over 10,000 pages) and saved to a temp directory in the server
when requested by a client.
I have updated this system and changed the pages that are saved to the
server as aspx - everything works fine and pages can be served - but
Its not impossible for a single client to request 100 plus pages in one
session - as each page is requested it is...
| |
by: Jack Fox |
last post by:
We are encountering a couple of problems with our ASP.NET / IIS 6.0
applications:
In each of 3 production environments we maintain a Windows Server 2003
machine running NTFS as a file server. The file server typically has 10s of
thousands of files on it. We've nver bothered to count, but I can imagine
that some environments excede 100,000 files.
Problem #1) We use the HttpContext Cache with a dependency on some given
file on the file...
|
by: Mike |
last post by:
Hi,
I am new to C and having problems with the following program.
Basically I am trying to read some files, loading data structures into
memory for latter searching.
I am trying to use structres and arrays of pointers to them.
I have gotten the program to compile with gcc on WinXP.
If the file i read doesnt have alot of records, it runs thru. But once
i add more, it dies. In this program i have 4 files setup to read. The
|
by: subsanta |
last post by:
My computer has so many problems and ive looked around on the internet and ive managed to fix some of them.
I know that i have a few viruses on my computer, but i cant get rid of them, in one case i think i have one on my ipod "boot.exe" and i cant get rid of it. All of the antivirus programs ive tried either dont pick it up or cant scan my ipod.
Den i have "copy.exe" which i dont know how to fix either.
Task manager and regedit were...
|
by: =?Utf-8?B?VG9kZCBEb2JtZXllcg==?= |
last post by:
I am working on developing a program using Visual Studio 2003 but am having
problems getting my program to find my GL.h and GLU.h, and I am guessing it
will have the same problems trying to link to the .lib and .dll files. What
do I all need to do to get this to properly compile and link. Here is what I
have done so far.
Inside my main project directory, I added a folder called "OpenGL" and
inside this folder are 3 sub-folders,...
|
by: Alexnb |
last post by:
Gerhard Häring wrote:
No, it didn't work, but it gave me some interesting feedback when I ran it
in the shell. Heres what it told me:
Traceback (most recent call last):
File "<pyshell#10>", line 1, in <module>
os.startfile("C:\Documents and Settings\Alex\My Documents\My
|
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: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
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
| |