g++: compilation of header file requested

  • Thread starter blueblueblue2005
  • Start date
B

blueblueblue2005

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
 
D

Dietmar Kuehl

blueblueblue2005 said:
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.
 
A

ambar.shome

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.
 
D

Dan Cernat

blueblueblue2005 said:
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
 
D

Dan Cernat

blueblueblue2005 said:
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
 
B

blueblueblue2005

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??
 
B

blueblueblue2005

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.
 
P

Paul

blueblueblue2005 said:
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
 
L

Leon Mergen

Hello,

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
 
P

Paul

Leon said:
Hello,




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.
 
D

Dietmar Kuehl

blueblueblue2005 said:
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.
 
M

msalters

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,681
Members
48,796
Latest member
Greg L.

Latest Threads

Top