473,500 Members | 1,929 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

g++: compilation of header file requested

Hi, here is an example I copied from Deitel C++ book. but when I
compile it, always get the above compilation error, no matter how I
change the include order, please help.

here is the files:
Note: this is to practice Proxy classes.

// Implementation.h

class Implementation {
public:
Implementation(int v) { this->v = v; }
void setValue(int v)
{
this->v = v;
}

int getValue() const
{
return v;
}

private:
int v;
};

// Interface.h

class Implementation;

class Interface {
public:
Interface(int);
~Interface();
void setValue(int);
int getValue() const;

private:
Implementation *ptr;
};

/* Interface.cpp */

#include "Implementation.h"
#include "Interface.h"

Interface::Interface(int v):ptr(new Implementation(v))
{ }

Interface::~Interface() { delete ptr; }

void Interface::setValue(int v){
ptr->setValue(v);
}

int Interface::getValue() const {
return ptr->getValue();
}

/* t.cpp */

#include <iostream>

using namespace std;

#include "Interface.h"
#include "Implementation.h"

int main(){
Interface i(5);

cout << i.getValue() << endl;
i.setValue(19);
cout << i.getValue() << endl;
return 0;
}

# ============ this is Makefile
# Makefile
a.out: Implementation.h Interface.h Interface.cpp t.cpp
g++ Implementation.h Interface.cpp t.cpp
clean:
rm -f a.out
cleanall:
rm -f *.h *.cpp a.out

Jul 23 '05 #1
12 12832
blueblueblue2005 wrote:
Hi, here is an example I copied from Deitel C++ book. but when I
compile it, always get the above compilation error, no matter how I
change the include order, please help.
Well, you might want to read the error message: it clearly says
that the compiler complains about the request that it should
compile a header file.
# ============ this is Makefile
# Makefile
a.out: Implementation.h Interface.h Interface.cpp t.cpp
g++ Implementation.h Interface.cpp t.cpp


The above line should read

g++ Interface.cpp t.cpp

instead.
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.eai-systems.com> - Efficient Artificial Intelligence
Jul 23 '05 #2
Hi,

May be you need to change your makefile. At the same time I dont
understand why r u defining the body of the functions within header
files like u did in Implementation.h. As far as my knowledge goes ".h"
files are for declaring function prototypes and variables only. We dont
write the body of the function within a header file. That might be the
reason why r u getting this compilation error.
blueblueblue2005 wrote:
Hi, here is an example I copied from Deitel C++ book. but when I
compile it, always get the above compilation error, no matter how I
change the include order, please help.

here is the files:
Note: this is to practice Proxy classes.

// Implementation.h

class Implementation {
public:
Implementation(int v) { this->v = v; }
void setValue(int v)
{
this->v = v;
}

int getValue() const
{
return v;
}

private:
int v;
};

// Interface.h

class Implementation;

class Interface {
public:
Interface(int);
~Interface();
void setValue(int);
int getValue() const;

private:
Implementation *ptr;
};

/* Interface.cpp */

#include "Implementation.h"
#include "Interface.h"

Interface::Interface(int v):ptr(new Implementation(v))
{ }

Interface::~Interface() { delete ptr; }

void Interface::setValue(int v){
ptr->setValue(v);
}

int Interface::getValue() const {
return ptr->getValue();
}

/* t.cpp */

#include <iostream>

using namespace std;

#include "Interface.h"
#include "Implementation.h"

int main(){
Interface i(5);

cout << i.getValue() << endl;
i.setValue(19);
cout << i.getValue() << endl;
return 0;
}

# ============ this is Makefile
# Makefile
a.out: Implementation.h Interface.h Interface.cpp t.cpp
g++ Implementation.h Interface.cpp t.cpp
clean:
rm -f a.out
cleanall:
rm -f *.h *.cpp a.out


Jul 23 '05 #3


blueblueblue2005 wrote:
Hi, here is an example I copied from Deitel C++ book. but when I
compile it, always get the above compilation error


[snip]
What error? There in no error included in the body of the message. Make
sure to include your problem in the *body* of the message not only in
the subject line.

/Dan

Jul 23 '05 #4


blueblueblue2005 wrote:
Hi, here is an example I copied from Deitel C++ book. but when I
compile it, always get the above compilation error


[snip]
What error? There is no error included in the body of the message. Make
sure to include your problem in the *body* of the message not only in
the subject line.

/Dan

Jul 23 '05 #5
you are right, g++ Interface.cpp t.cpp works. but why when I include
Implementation.h in g++ command, it complains that it should compile a
header file??

Jul 23 '05 #6
the reason I define the body of functions within header file is the
body is too small, as you see, just one line. and the reason I got
compilation of header file error is my Makefile.

Dietmar's reply indicated the error, I just dont know why I can't
include Implementation.h in g++ command.

Jul 23 '05 #7
blueblueblue2005 wrote:
you are right, g++ Interface.cpp t.cpp works. but why when I include
Implementation.h in g++ command, it complains that it should compile a
header file??


You are not supposed to send header files to the compiler. Header files
are generally used to only define an interface to an implementation
file, object file, or library.
It is common to implement small functions and declare them 'inline' in
header files for convenience, or to declare bodies of small class member
functions so they don't get lost among much larger functions declared in
the counterpart cpp files.
Just send 'cpp' and 'o' files to the compiler, it will find the headers
through your #include directives.
--Paul
Jul 23 '05 #8
Make sure to include your problem in the *body* of the message not only in
the subject line.


Ok, :-)

Jul 23 '05 #9
Hello,

On Mon, 2005-07-04 at 11:43 -0700, am*********@gmail.com wrote:
As far as my knowledge goes ".h" files are for declaring function
prototypes and variables only. We dont write the body of the function
within a header file. That might be the reason why r u getting this
compilation error.


As far as my knowledge goes, this is being done on a pretty common base.
For example, the Spirit parser ( http://spirit.sf.net/ ) is written
entirely in header files.

I know that header files are supposed to be for defining functions, but
isn't this rather for /other/ classes that include this class ? I mean,
as I see it, the strict separation between header files and source files
is because that way a compiler can look at a header, know what functions
a certain class accepts, and then parse one class accordingly... you do
not need a pure definition of the class YET... but what's so much
against it, if it's possible ? Sure, if a class includes a class that
tries to access member functions of the class including it (sheesh, what
a sentence :)), sure, you need separation between header and source so
it can be solved at compile-time... but why do you need to do this all
the time, especially for the simple functions the parent post is using ?

Regards,

Leon Mergen

Jul 23 '05 #10
Leon Mergen wrote:
Hello,

On Mon, 2005-07-04 at 11:43 -0700, am*********@gmail.com wrote:
As far as my knowledge goes ".h" files are for declaring function
prototypes and variables only. We dont write the body of the function
within a header file. That might be the reason why r u getting this
compilation error.

As far as my knowledge goes, this is being done on a pretty common base.
For example, the Spirit parser ( http://spirit.sf.net/ ) is written
entirely in header files.


The Spirit parser is implemented using template meta-programming
techniques, which is a different beast altogether from
header/implementation separation.
The STL is implemented using "header" files, they just contain template
definitions and don't have the .h extension. They also don't need to be
explicitly sent to the compiler (but referenced via #include instead)
because of the way templates work during compile time.

I know that header files are supposed to be for defining functions, but
isn't this rather for /other/ classes that include this class ? I mean,
as I see it, the strict separation between header files and source files
is because that way a compiler can look at a header, know what functions
a certain class accepts, and then parse one class accordingly... you do
not need a pure definition of the class YET... but what's so much
against it, if it's possible ? Sure, if a class includes a class that
tries to access member functions of the class including it (sheesh, what
a sentence :)), sure, you need separation between header and source so
it can be solved at compile-time... but why do you need to do this all
the time, especially for the simple functions the parent post is using ?

Regards,

Leon Mergen

Jul 23 '05 #11
blueblueblue2005 wrote:
you are right, g++ Interface.cpp t.cpp works. but why when I include
Implementation.h in g++ command, it complains that it should compile a
header file??


The gcc/g++ determines the kind of file it is supposed to process
according to the extension (and possibly other indicators). It
knows about .c (C source files), .C, .cxx, .cpp, .cc, and a few
more (C++ source files), .o (object files, i.e. binary files
representing the compiled but not linked result of the compiler;
see the -c option for more details), .a (libraries, i.e. collections
of object files), .so (shared libraries, i.e. collections of object
files prepared for swift reference resolution), and a bunch of other
files. However, it does not know how to process .h, .hpp, .hxx, .H,
etc. because these only contain declaration and no definitions and
it thus does not make any sense to compile them (the result would
be empty and if it is not it is either useless or does even cause
problems).

Header files contain declarations which are shared between multiple
source files. It would be equivalent for the compiler to just
provide the declarations in each individual file. Of course, this
would be a maintenance nightmare and it is much more reasonable to
factor common declarations into a single separate file.
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.eai-systems.com> - Efficient Artificial Intelligence
Jul 23 '05 #12


Paul schreef:
The Spirit parser is implemented using template meta-programming
techniques, which is a different beast altogether from
header/implementation separation.
Actually, the main difference is that separate template implementations
require the use of 'export', which isn't very well supported (*cough*).
Hence, template funtions are often inlined, and like other inline
functions can be in headers.
The STL is implemented using "header" files, they just contain template
definitions and don't have the .h extension. They also don't need to be
explicitly sent to the compiler (but referenced via #include instead)
because of the way templates work during compile time.


The STL is nowadays (since 98) part of the Standard Library, and
implemented via compiler magic. On some compilers, that magic means
"header files without .h" but this is not universal.

You're often right that they don't need to be sent to the compiler.
At least all compilers I know are smart enough to find them, but I
know some compilers can be configured to select different library
implementations.

HTH,
Michiel Salters

Jul 23 '05 #13

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

Similar topics

3
2204
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... ...
6
10958
by: yezi | last post by:
Hi: I code a main function in one file "nlp.c" code defination of variabla in one file "nlp.h" I use " gcc nlp.c -o nlp.out ; no error message; I use :
0
3908
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen....
2
3677
by: Annie | last post by:
hello guys, I have a header file in the form of user control (.ascx) that all my pages inherit from it. I have a textbox that i need to access it from all the pages actually i show there user...
5
5840
by: Mikael S. H. | last post by:
Header file compilation I'm coding a small irc bot, and I've noticed that compilation takes very long when I add certain header files (compared to compilation time without). I've tried to find...
8
7614
by: nishit.gupta | last post by:
I was having a problem with template class memer function definition , so i serched the net and find that template class member fuction definition should be in header file else compilation will...
6
3420
by: bobby | last post by:
hi group, Does the header file size or number in include(s) effect the size of executable file? In other world if i chose a large header file and include it with my source file does it increase...
1
2055
by: jacek.dziedzic | last post by:
Hi! I have a program that #includes a header file of an external library. If the environment is set-up correctly, this file is found in the include path and everything works. Now, assume...
0
7136
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
1
6906
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
7397
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
5490
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,...
1
4923
isladogs
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...
0
3106
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1430
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 ...
1
672
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
316
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.