Qbasicnews.com
November 27, 2021, 06:00:33 PM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: Back to Qbasicnews.com | QB Online Help | FAQ | Chat | All Basic Code | QB Knowledge Base
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: [C++] Help with header files  (Read 5075 times)
aardvark
Senior Member
**
Posts: 228



« on: January 15, 2004, 10:25:13 AM »

Help! How do C++ header files work? Say I have the following three files:

main.cpp
Code:

#include "title.h"

int main()
{
    title();
    return 0;
}


title.h
Code:

?????


title.cpp
Code:

#include <stdio.h>
#include <iostream.h>

void title()
{
    cout << "This is a procedure that I would like to use several times over in several different modules, and so have an header file pointing to it."
}


How do I get the header file to point to the title procedure inside title.cpp? Ah, languages devised by societies of intellectual eletists are so refreshing (read: f... messed up.).

PS: 486 DX/100 with 8MB RAM and DJGPP compiler with rhide IDE, if it matters.
Logged

In a race between a rock and a pig, don't varnish your clams."   -- "Dilbert"
L_O_J
Member
*
Posts: 62



« Reply #1 on: January 15, 2004, 11:06:14 AM »

well write this inside the title.h

void title();

done  Cheesy
Logged
aardvark
Senior Member
**
Posts: 228



« Reply #2 on: January 15, 2004, 11:10:19 AM »

Are you sure? How does it know what file TITLE is in?
Logged

In a race between a rock and a pig, don't varnish your clams."   -- "Dilbert"
L_O_J
Member
*
Posts: 62



« Reply #3 on: January 15, 2004, 11:17:02 AM »

I dont know about rhide  Sad

but I use devC and I just include the file title.cpp in the project so when it compiled the title.cpp is also compiled  Cheesy

well you always can try
#include"title.cpp" after #include "title.h"
Logged
na_th_an
*/-\*
*****
Posts: 8244



WWW
« Reply #4 on: January 15, 2004, 11:58:51 AM »

Quote from: "aardvark"
Are you sure? How does it know what file TITLE is in?


It doesn't need to know that. That .h just makes sure that the symbol "title" is available when compiling, so the compiler doesn't complain if it doesn't find a correct definition of "title" in the file it is compiling. The linker will take care and will transform the calls to title correctly.

Btw, afaik in GCC compatible compilers C++ stuff is .cc, not .cpp. Anyhow, I don't like including .cpp files as it can make your compiler go nuts. Use .h and let the linker locate stuff instead of the compiler. A "include" just takes a file and pastes it inside another in compiling time.

Also, note that Rhide is just the ide. The compiler is DJGPP. DevC++ is also the ide, the compiler is MinGW. Both DJGPP and MinGW are GCC ports. GCC is the GNU C Compiler. GNU means "GNU is Not Unix" Tongue
Logged

SCUMM (the band) on Myspace!
ComputerEmuzone Games Studio
underBASIC, homegrown musicians
[img]http://www.ojodepez-fanzine.net/almacen/yoghourtslover.png[/i
aardvark
Senior Member
**
Posts: 228



« Reply #5 on: January 15, 2004, 10:04:32 PM »

So, basically, use .cc files instead of .cpp files and just have all the files I need added to my Project? Did that, and got: "multiple definition of type title(void)".

Quote

Also, note that Rhide is just the ide. The compiler is DJGPP. DevC++ is also the ide, the compiler is MinGW. Both DJGPP and MinGW are GCC ports. GCC is the GNU C Compiler. GNU means "GNU is Not Unix"


Yeah... that's what I said. rhide IDE. Like this:
Quote

DJGPP compiler with rhide IDE
Logged

In a race between a rock and a pig, don't varnish your clams."   -- "Dilbert"
LooseCaboose
I hold this place together
*****
Posts: 981



« Reply #6 on: January 15, 2004, 11:26:58 PM »

C, unlike QBasic is picky about symbols being defined before they are used. Before you can use a variable you need to define it, for example:
Code:

int x;

x = 5;


The same goes for functions (although C is a little less picky about this, you should always define functions before using them):
Code:

void func(void);

void func(void) {
  /* Blah */
}

int main() {
  func();
}


Because you may want to be able to call a function from several different files, it is nicer to put the function definitions insider header files and then include them in each file that either contains the function or calls it.

Other things you may want to place in header files are typedefs, structs, external variables, preprocessor macros and constants.

Quote from: "na_th_an"

Btw, afaik in GCC compatible compilers C++ stuff is .cc, not .cpp.

Gcc can accept the following extensions (at least) for C++: .cpp, .cc, .cp, .cxx, .c++ and .C
Logged

esus saves.... Passes to Moses, shoots, he scores!
na_th_an
*/-\*
*****
Posts: 8244



WWW
« Reply #7 on: January 16, 2004, 07:49:04 AM »

Quote from: "aardvark"
So, basically, use .cc files instead of .cpp files and just have all the files I need added to my Project? Did that, and got: "multiple definition of type title(void)".


No. You have to create a project and add the files to it. Look up the project window. (View->Project, I think)

Loosecaboose: I didn't know that Tongue
Logged

SCUMM (the band) on Myspace!
ComputerEmuzone Games Studio
underBASIC, homegrown musicians
[img]http://www.ojodepez-fanzine.net/almacen/yoghourtslover.png[/i
aardvark
Senior Member
**
Posts: 228



« Reply #8 on: January 16, 2004, 08:41:21 AM »

Quote

No. You have to create a project and add the files to it. Look up the project window. (View->Project, I think)


But I did that...  :normal:
Logged

In a race between a rock and a pig, don't varnish your clams."   -- "Dilbert"
na_th_an
*/-\*
*****
Posts: 8244



WWW
« Reply #9 on: January 16, 2004, 09:08:43 AM »

You mustn't include *.h in the project.
Logged

SCUMM (the band) on Myspace!
ComputerEmuzone Games Studio
underBASIC, homegrown musicians
[img]http://www.ojodepez-fanzine.net/almacen/yoghourtslover.png[/i
aardvark
Senior Member
**
Posts: 228



« Reply #10 on: January 16, 2004, 09:14:10 AM »

Hurrah! Now I can link to my other files that don't work Wink. More specifically, the whole thing doesn't work because I can't get those damn classes working. But I'll sort that out.
Logged

In a race between a rock and a pig, don't varnish your clams."   -- "Dilbert"
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines Valid XHTML 1.0! Valid CSS!