Very basic question

D

Der Andere

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

Thomas Matthews

Der said:
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
 
J

John Harrison

Der Andere said:
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
 
A

Allan Bruce

Der Andere said:
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
 
K

Karl Heinz Buchegger

Der said:
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.
 
G

Gernot Frisch

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
 
D

Der Andere

Say I have a project containing four files: a.cpp, a.h, b.cpp and b.h,
where

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


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
 
J

John Harrison

Der Andere said:
Yes, indeed that causes less problems.


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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
DewittMill
Top