473,397 Members | 2,028 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Problem with writing my own header file...

Hi,
Well, I'm still a newbie to C++, so excuse my maybe (probably???) stupid
question...btw, I use gcc 2.95.2.

Here are my (test) funtions:

mainfunc.cpp:
#include <iostream>
#include "func1.h" //so it looks in the same dir, right?
int main(){
int a=42;
func1(a); }

func1.cpp:
#include <iostream>
#include "func1.h" /* I read you should also include it here... */
void func1(int i) {
cout << i << endl; }

func1.h:
void func1(int i);

My Problem is that when I try to compile it, it gives me an "undefined
reference to 'func1(int)' error and I don't know how to get rid of it. I
tried to put the files everywhere, like in the "include"-folder or others
folders that I set with path.
What is my mistake??? Thank you in advance, I really appreciate it!

Regards
Markus

Jul 19 '05 #1
7 4296
WW
Markus wrote:
Hi,
Well, I'm still a newbie to C++, so excuse my maybe (probably???)
stupid question...btw, I use gcc 2.95.2.

Here are my (test) funtions:

mainfunc.cpp:
#include <iostream>
using namesapce std;
#include "func1.h" //so it looks in the same dir, right?
int main(){
int a=42;
func1(a); }

func1.cpp:
#include <iostream>
using namesapce std;
#include "func1.h" /* I read you should also include it here... */
void func1(int i) {
cout << i << endl; }

func1.h:
void func1(int i);

My Problem is that when I try to compile it, it gives me an "undefined
reference to 'func1(int)' error and I don't know how to get rid of
it. I tried to put the files everywhere, like in the "include"-folder
or others folders that I set with path.
What is my mistake??? Thank you in advance, I really appreciate it!


You need to LINK func1.o into your executable! Please get a gcc/g++ manual
and read about it. It is _using_ your compiler and not about the language
so - strictly speaking - it is off topic here. Byt I am pretty sure that a
million tutorials exist out there for gcc/g++.

--
WW aka Attila
Jul 19 '05 #2
<Markus>
My Problem is that when I try to compile it, it gives me an "undefined
reference to 'func1(int)' error and I don't know how to get rid of it. I
tried to put the files everywhere, like in the "include"-folder or others
folders that I set with path.
What is my mistake??? Thank you in advance, I really appreciate it!

</>

JT> I really don't want to define all the hundreds of error message strings
in
JT> every .cpp file. :-( What do i wrong?
JT>
JT> Any hint appreciated.

You can use this very simple scheme if you like:

//file First.cpp
class First{public:First(){cout<<"phew";};

//file Second.cpp
class Second:public First{public:Second():First(){cout<<"gosh";}};

//file Third.cpp
class Third
{
public:
vector<First>vf;
First f;
Second s;
Third(){cout<<"well-well";}
};

//file main.cpp
#include <iostream.h>
#include <vector.h>
#include "First.cpp"
#include "Second.cpp"
#include "Third.cpp"

int main(int argc,char**argv)
{
First f;
Second s;
Third t;
return 0;
}

All the includes are summarized in file main.cpp. The only
file that gets compiled is main.cpp and consequently all
other cpp files. What actually happens is that the precompiler
expands all the included files into main.cpp and then that expanded
file is compiled. So no complicated makefiles and no double
includes, at the cost of longer compilation time. Stale .obj files
can be a serious source of bugs. With this approach you are
sure to always work on your most recent version.

-X


Jul 19 '05 #3
"Markus" <Ma***********@nospam.de> writes:
Hi,
Well, I'm still a newbie to C++, so excuse my maybe (probably???) stupid
question...btw, I use gcc 2.95.2.

Here are my (test) funtions:

mainfunc.cpp:
#include <iostream>
#include "func1.h" //so it looks in the same dir, right?
int main(){
int a=42;
func1(a); }

func1.cpp:
#include <iostream>
#include "func1.h" /* I read you should also include it here... */
void func1(int i) {
cout << i << endl; }

func1.h:
void func1(int i);

My Problem is that when I try to compile it, it gives me an "undefined
reference to 'func1(int)' error and I don't know how to get rid of it.


Undefined reference means that the *linker* could not find your function -
you have to link in every source file in your project.
compile it with

g++ -Wall -ansi -pedantic mainfunc.cpp func1.cpp

and it should work.

HTH & kind regards
frank

--
Frank Schmitt
4SC AG phone: +49 89 700763-0
e-mail: frankNO DOT SPAMschmitt AT 4sc DOT com
Jul 19 '05 #4
"Agent Mulder" <mb*******************@home.nl> wrote in message
news:bl**********@news1.tilbu1.nb.home.nl...
You can use this very simple scheme if you like:

//file First.cpp
class First{public:First(){cout<<"phew";};

//file Second.cpp
class Second:public First{public:Second():First(){cout<<"gosh";}};

//file Third.cpp
class Third
{
public:
vector<First>vf;
First f;
Second s;
Third(){cout<<"well-well";}
};

//file main.cpp
#include <iostream.h>
#include <vector.h>
#include "First.cpp"
#include "Second.cpp"
#include "Third.cpp"

int main(int argc,char**argv)
{
First f;
Second s;
Third t;
return 0;
}

All the includes are summarized in file main.cpp. The only
file that gets compiled is main.cpp and consequently all
other cpp files. What actually happens is that the precompiler
expands all the included files into main.cpp and then that expanded
file is compiled. So no complicated makefiles and no double
includes, at the cost of longer compilation time. Stale .obj files
can be a serious source of bugs. With this approach you are
sure to always work on your most recent version.

-X


Thank you! Yes, your solution does work, even though it is not the approach
I was using. This way I don't need the header file at all, but the way I see
it (from my newbie point of view), I can't pass on values that easily,
right?
Jul 19 '05 #5
"Frank Schmitt" <in*****@seesignature.info> wrote in message
news:4c************@scxw21.4sc...
Undefined reference means that the *linker* could not find your function -
you have to link in every source file in your project.
compile it with

g++ -Wall -ansi -pedantic mainfunc.cpp func1.cpp

and it should work.

HTH & kind regards
frank


It compiled alright but did not create an executable??? Usually I use
Textpad, so I don't really know the command settings. WW said there should
be millions of tutorials about that, couldn't find one so far but I'll keep
looking. Thanks!
Jul 19 '05 #6
"Markus" <Ma***********@nospam.de> writes:
"Frank Schmitt" <in*****@seesignature.info> wrote in message
news:4c************@scxw21.4sc...
Undefined reference means that the *linker* could not find your function -
you have to link in every source file in your project.
compile it with

g++ -Wall -ansi -pedantic mainfunc.cpp func1.cpp

and it should work.

HTH & kind regards
frank


It compiled alright but did not create an executable??? Usually I use
Textpad, so I don't really know the command settings. WW said there should
be millions of tutorials about that, couldn't find one so far but I'll keep
looking. Thanks!


I find this hard to believe - searching for "gcc tutorial" on google turns up
zillions of links, among them

http://users.actcom.co.il/~choo/lupg...unning_the_exe

regards
frank

--
Frank Schmitt
4SC AG phone: +49 89 700763-0
e-mail: frankNO DOT SPAMschmitt AT 4sc DOT com
Jul 19 '05 #7
"Frank Schmitt" <in*****@seesignature.info> wrote in message
news:4c************@scxw21.4sc...
I find this hard to believe - searching for "gcc tutorial" on google turns up zillions of links, among them

http://users.actcom.co.il/~choo/lupg...unning_the_exe
regards
frank


Oh, no - you're absolutly right! There are plenty of tutorials out there,
what I meant was a tutorial for gcc in combination with textpad. I got it as
far as compiling and all, but I don't know how to do it with including other
funtions...

The class solutions works, but I'd believe there should be an easier way.
But on the other hand, I don't have a clue, so I'm probably wrong anyways.

Thank you again for the reply, though. ;-)
Jul 19 '05 #8

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

Similar topics

2
by: Randy Jackson | last post by:
Hello all. Okay, this seems really stupid, but it's driving me up the wall. I have a simple script I've written to log some information to a text file. Everything seems to be okay, the code...
6
by: vasilijepetkovic | last post by:
Hello All, I have a problem with the program that should generate x number of txt files (x is the number of records in the file datafile.txt). Once I execute the program (see below) only one...
25
by: Xah Lee | last post by:
Python Doc Problem Example: gzip Xah Lee, 20050831 Today i need to use Python to compress/decompress gzip files. Since i've read the official Python tutorial 8 months ago, have spent 30...
11
by: Abhishek | last post by:
I have a problem transfering files using sockets from pocket pc(.net compact c#) to desktop(not using .net just mfc and sockets 2 API). The socket communication is not a issue and I am able to...
3
by: John R. Delaney | last post by:
I am running in debugging mode after a clean C++ compilation under .NET 2003. In a BIG loop (controlled many levels up in the call stack), I open a file with fopen using the "a" option. Then I write...
0
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....
3
by: webEater | last post by:
Hey, I am writing a file that reads in an external file in the web and prints it out including the response header of the http protocol. I do this to enable cross domain XMLHttpRequests. I...
6
by: arne.muller | last post by:
Hello, I've come across some problems reading strucutres from binary files. Basically I've some strutures typedef struct { int i; double x; int n; double *mz;
0
by: george585 | last post by:
Hello! I am new to network programming, and understand just basics. Using some sample code, and having read documentation, I managed to create a simple app in C# and VB.NET. The application is...
9
by: xiao | last post by:
It always dumped when I tried to run it... But it compiles OK. What I want to do is to do a test: Read information from a .dat file and then write it to another file. The original DAT file is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
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...
0
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,...
0
Oralloy
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,...
0
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
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,...
0
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...

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.