Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Very basic question

Reply
Thread Tools

Very basic question

 
 
Der Andere
Guest
Posts: n/a
 
      06-23-2004
Say I have a project containing four files: a.cpp, a.h, b.cpp and b.h, where
a.h is included in a.cpp and b.h is included in b.cpp.
Can I make cross-references from a to b. For instance, can I use a class
defined in b.cpp in a.cpp? Or do I have to add '#include "b.cpp" ' into
a.cpp?


 
Reply With Quote
 
 
 
 
Thomas Matthews
Guest
Posts: n/a
 
      06-23-2004
Der Andere wrote:
> Say I have a project containing four files: a.cpp, a.h, b.cpp and b.h, where
> a.h is included in a.cpp and b.h is included in b.cpp.
> Can I make cross-references from a to b. For instance, can I use a class
> defined in b.cpp in a.cpp? Or do I have to add '#include "b.cpp" ' into
> a.cpp?
>
>


If you want to use a class declared in a.hpp in file b.cpp, you just
have to include a.hpp within file b.cpp.


--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      06-23-2004

"Der Andere" <> wrote in message
news:cbc08b$doa$00$...
> Say I have a project containing four files: a.cpp, a.h, b.cpp and b.h,

where
> a.h is included in a.cpp and b.h is included in b.cpp.
> Can I make cross-references from a to b. For instance, can I use a class
> defined in b.cpp in a.cpp?


Yes, of course, it would be very hard to write programs in seperate files
without that facility.

> Or do I have to add '#include "b.cpp" ' into
> a.cpp?
>


No that would be a bad idea. Most likely you need to ' #include "b.h" ' in
a.cpp.

But maybe your problem is with something else entirely, why not post the
code?

john


 
Reply With Quote
 
Allan Bruce
Guest
Posts: n/a
 
      06-23-2004

"Der Andere" <> wrote in message
news:cbc08b$doa$00$...
> Say I have a project containing four files: a.cpp, a.h, b.cpp and b.h,

where
> a.h is included in a.cpp and b.h is included in b.cpp.
> Can I make cross-references from a to b. For instance, can I use a class
> defined in b.cpp in a.cpp? Or do I have to add '#include "b.cpp" ' into
> a.cpp?
>
>


You should declare the classes in the .h files and define the methods in the
..cpp files. That way, you can include "a.h" to access the classes in a.cpp
from any other file. e.g.


// a.h
class A
{
void test();
};


// a.cpp
void A::test()
{
std::cout << "Class A test\n";
}


// b.h
class B
{
void doSomething();
};


// b.cpp
#include "a.h"

void B::doSomething()
{
std::cout << "In B::doSomething()\n";
A instanceOfClassA;
instanceOfClassA.test();
}



Hope that helps
Allan


 
Reply With Quote
 
Karl Heinz Buchegger
Guest
Posts: n/a
 
      06-23-2004
Der Andere wrote:
>
> Say I have a project containing four files: a.cpp, a.h, b.cpp and b.h, where
> a.h is included in a.cpp and b.h is included in b.cpp.
> Can I make cross-references from a to b. For instance, can I use a class
> defined in b.cpp in a.cpp?


Yes.

> Or do I have to add '#include "b.cpp" ' into
> a.cpp?


No. You include the header file b.h
It (should) contain everything needed to make a.cpp
compileable. For this the compiler only needs to know
that some class exists and what public members it has (in
order to check for function names, argument lists, etc). All
of this is written down in the header file.

You then compile a.cpp. compile b.cpp and link the result of
both compile steps to form the executable.

--
Karl Heinz Buchegger

 
Reply With Quote
 
Gernot Frisch
Guest
Posts: n/a
 
      06-23-2004
Is this what you want to do:

// A.h
class A
{
B m_B;
};

// B.h
class B
{
AssignNewParent(A& newA);
};


Then you can do these:
- let B know that A exists and include B.h into A.h

// A.h
#inlcude "B.h" // Get full information about B to make an instance
....
// B.h
class A; // Simply let the compiler know that it exists
....


Post code that doesn't work if this didn't help.

HTH,
Gernot




 
Reply With Quote
 
Der Andere
Guest
Posts: n/a
 
      06-23-2004
> > Say I have a project containing four files: a.cpp, a.h, b.cpp and b.h,
> where
> > a.h is included in a.cpp and b.h is included in b.cpp.
> > Can I make cross-references from a to b. For instance, can I use a class
> > defined in b.cpp in a.cpp?

>
> Yes, of course, it would be very hard to write programs in seperate files
> without that facility.
>
> > Or do I have to add '#include "b.cpp" ' into
> > a.cpp?
> >

>
> No that would be a bad idea. Most likely you need to ' #include "b.h" ' in
> a.cpp.


Yes, indeed that causes less problems.

> But maybe your problem is with something else entirely, why not post the
> code?


It is more than a few hundred lines. I developed the two files independently
from each other but now they need to interact. Whenever I tried to include
one cpp file within the other I got lots of errors because I used the same
libraries in both cpp files.
I did not have the idea myself to include only the headers. I thought this
would not work because there is no reference to b.cpp if I include b.h
within a.cpp. Does it work because b.cpp is in my project as well? Or does
the compiler *guess* that there must be a b.cpp if there is a b.h?

However, it works fine now, thanks!!

Regards,
Matthias





 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      06-23-2004

"Der Andere" <> wrote in message
news:cbc32j$4t6$06$...
> > > Say I have a project containing four files: a.cpp, a.h, b.cpp and b.h,

> > where
> > > a.h is included in a.cpp and b.h is included in b.cpp.
> > > Can I make cross-references from a to b. For instance, can I use a

class
> > > defined in b.cpp in a.cpp?

> >
> > Yes, of course, it would be very hard to write programs in seperate

files
> > without that facility.
> >
> > > Or do I have to add '#include "b.cpp" ' into
> > > a.cpp?
> > >

> >
> > No that would be a bad idea. Most likely you need to ' #include "b.h" '

in
> > a.cpp.

>
> Yes, indeed that causes less problems.
>
> > But maybe your problem is with something else entirely, why not post the
> > code?

>
> It is more than a few hundred lines. I developed the two files

independently
> from each other but now they need to interact. Whenever I tried to include
> one cpp file within the other I got lots of errors because I used the same
> libraries in both cpp files.


Huh?

> I did not have the idea myself to include only the headers. I thought this
> would not work because there is no reference to b.cpp if I include b.h
> within a.cpp.


The whole point of header is that you put things in them that you need to
share between more that one cpp file. That is why you include headers in cpp
files.

> Does it work because b.cpp is in my project as well? Or does
> the compiler *guess* that there must be a b.cpp if there is a b.h?


The compiler doesn't guess anything. You must include a.cpp and b.cpp in
your project.

>
> However, it works fine now, thanks!!
>


john


 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
I found some very odd behaviour in Python's very basic types Sunjay Varma Python 4 03-10-2011 05:05 PM
A very **very** basic question mdh C Programming 57 09-26-2008 03:25 PM
very very basic question aghazalp Python 6 04-02-2006 09:35 PM
Very very very basic question Peter C Programming 14 02-14-2005 09:46 AM
very Very VERY dumb Question About The new Set( ) 's Raymond Arthur St. Marie II of III Python 4 07-27-2003 12:09 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57