Connecting Tech Pros Worldwide Help | Site Map
 
 
LinkBack Thread Tools Search this Thread
  #1  
Old September 26th, 2007, 11:45 PM
bob
Guest
 
Posts: n/a
Default Modular programming

I have made a file: box.h that have some structs, simple functions
etc.

I have then made another file: print.h that have a class 'print' with
methods to print the content from 'box' (content of various structs,
fields etc) which currently does not work.

in box.h I write:

#include "print.h"

And then I make a new print object like 'Print p' and it works fine
when I from 'box.h' do something like:

p.printLine()

But how do I make 'print.h' aware of the types and struct in box.h?

In box.h I have the struct 'Line'. I would like to pass this struct to
a Print object like:

Line test;
p.printLine2(test);


Therefore I have in print.h written:

#include "box.h"

but when I define the function in print.h:

void printLine2(Line a) {
std::cout << "test\n";
}

I just get:

error: 'Line' has not been declared


Why can I use print.h in box.h but not make print.h aware of the types
in box.h?

  #2  
Old September 27th, 2007, 02:45 AM
Adam Nielsen
Guest
 
Posts: n/a
Default Re: Modular programming

in box.h I write:
Quote:
>
#include "print.h"
>
Quote:
Therefore I have in print.h written:
>
#include "box.h"
>
There's your problem - every time the compiler reads in one .h file,
you're telling it to look in the other one. Your compiler is obviously
smart enough to realise the problem, but the side effect is that your
function definition is being read before the definition of Line.

The real issue of course is that you're putting code into the .h files.
The .h files are only meant for definitions. If you put code into .h
files you're not actually writing modular programs, as after all the
#include directives have been processed you will only have one really
big module.

What you need to do is to split the code into multiple .cpp files, as
each .cpp file is normally treated as one module, regardless of how many
..h files it includes.

There should be plenty of tutorials on the web explaining how to do this
in more detail.

Cheers,
Adam.
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 205,338 network members.