473,769 Members | 6,473 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::Inte rface(int v):ptr(new Implementation( v))
{ }

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

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

int Interface::getV alue() 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 12875
blueblueblue200 5 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.co m> <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.
blueblueblue200 5 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::Inte rface(int v):ptr(new Implementation( v))
{ }

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

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

int Interface::getV alue() 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


blueblueblue200 5 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


blueblueblue200 5 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
blueblueblue200 5 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*********@gma il.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

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

Similar topics

3
2228
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
6
10992
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
3940
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. It is almost like it is trying to implement it's own COM interfaces... below is the header, and a link to the dll+code: Zip file with header, example, and DLL:...
2
3699
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 details there and needs to update it. So i set this textbox as Public and Static. The code compiles fine and the textbox is accessable to all pages however i have the following compilation errr: Compiler Error Message: CS0176: Static member...
5
5860
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 out if it is possible to compile a header file, yet I have only found that it is possible, but not how it is done. I have RTFM, STFW and searched USENET, yet I have only found a document saying "just write `g++ file.h`, and it'll work".
8
7632
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 be successful but not linking. Can somebody help me in telling the exact reason why compiler cannot find the reference of template class member function definition defined in cpp file?? following problem is not linking (undefined reference of...
6
3436
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 the size of the executable outcome at the end? thanks
1
2068
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 that a user forgot to either install the external library
0
9589
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10049
jinu1996
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...
0
9865
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 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...
0
8873
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, 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...
1
7413
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 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...
0
6675
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();...
0
5309
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...
2
3565
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.