Velocity Reviews > C++ > Very basic question

# 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?

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

John Harrison
Guest
Posts: n/a

 06-23-2004

"Der Andere" <(E-Mail Removed)> wrote in message
news:cbc08b$doa$00$(E-Mail Removed)-online.com... > 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 Allan Bruce Guest Posts: n/a  06-23-2004 "Der Andere" <(E-Mail Removed)> wrote in message news:cbc08b$doa$00$(E-Mail Removed)-online.com...
> 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

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
http://www.velocityreviews.com/forums/(E-Mail Removed)

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

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

John Harrison
Guest
Posts: n/a

 06-23-2004

"Der Andere" <(E-Mail Removed)> wrote in message
news:cbc32j$4t6$06\$(E-Mail Removed)-online.com...
> > > 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

 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 OffTrackbacks are On Pingbacks are On Refbacks are Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post Sunjay Varma Python 4 03-10-2011 05:05 PM mdh C Programming 57 09-26-2008 03:25 PM aghazalp Python 6 04-02-2006 09:35 PM Peter C Programming 14 02-14-2005 09:46 AM Raymond Arthur St. Marie II of III Python 4 07-27-2003 12:09 AM

Advertisments