473,325 Members | 2,870 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

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?
Jul 22 '05 #1
7 1230

"Der Andere" <ma**************@gmx.net> wrote in message
news:cb*************@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
Jul 22 '05 #2
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

Jul 22 '05 #3

"Der Andere" <ma**************@gmx.net> wrote in message
news:cb*************@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
Jul 22 '05 #4
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
kb******@gascad.at
Jul 22 '05 #5
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


Jul 22 '05 #6
> > 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

Jul 22 '05 #7

"Der Andere" <ma**************@gmx.net> wrote in message
news:cb*************@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
Jul 22 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Tom | last post by:
Hey all, I've been planning to get myself started with DocBook for quite some time now, so when I unexpectedly encountered a task for which DocBook might actually be very useful, I thought I'd...
30
by: Vla | last post by:
why did the designers of c++ think it would be more useful than it turned out to be?
5
by: Lee David | last post by:
I went to the sun site and downloaded what I hope is the development part of java. I downloaded JDK5 with Netbeans. I installed it and now have a folder in my program group "Netbeans". Is that...
10
by: Jason Curl | last post by:
Greetings, I have an array of 32 values. This makes it extremely fast to access elements in this array based on an index provided by a separate enum. This array is defined of type "unsigned long...
6
by: msnews.microsoft.com | last post by:
Hello All, I am very new to ASP.NET and I have a basic question. Can somebody please explain? I have an .aspx Web Page with a textbox control. When the Page initially loads I am calling a...
8
by: pamelafluente | last post by:
Hi, I would like to get some advice. I know enough vb.net on win apps, but I have never worked on web applications and asp.net. I would appreciate if you could indicate the most basic...
6
by: aghazalp | last post by:
hi guys, this would be the most basic question ever...I am not a programmer but I am trying to learn programming in python...I was reading John Zelle's text book and instructed me to make .py file...
17
by: blueapricot416 | last post by:
This is a very basic question -- but I can't find the answer after looking for 20 minutes. If you code something like: function set_It() { setTimeout('Request_Complete("apple", -72)',5000) }...
7
by: Bruno43 | last post by:
Hi I am trying to learn Visual Basic and I am getting everything for the most part, mainly because of my ability to read code like a book, but my question is what is the best way to Navigate through...
56
by: mdh | last post by:
As I begin to write more little programs without the help of the exercises, little things pop up that I need to understand more fully. Thus, below, and although this is not the exact code, the...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.