Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Very basic question (http://www.velocityreviews.com/forums/t283956-very-basic-question.html)

Der Andere 06-23-2004 01:25 PM

Very basic question
 
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 06-23-2004 01:30 PM

Re: Very basic question
 
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 06-23-2004 01:34 PM

Re: Very basic question
 

"Der Andere" <matieuloeschmich@gmx.net> wrote in message
news:cbc08b$doa$00$1@news.t-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 06-23-2004 01:36 PM

Re: Very basic question
 

"Der Andere" <matieuloeschmich@gmx.net> wrote in message
news:cbc08b$doa$00$1@news.t-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 06-23-2004 01:37 PM

Re: Very basic question
 
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
kbuchegg@gascad.at

Gernot Frisch 06-23-2004 01:43 PM

Re: Very basic question
 
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 06-23-2004 02:13 PM

Re: Very basic question
 
> > 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 06-23-2004 02:19 PM

Re: Very basic question
 

"Der Andere" <matieuloeschmich@gmx.net> wrote in message
news:cbc32j$4t6$06$1@news.t-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




All times are GMT. The time now is 03:59 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.