Very basic question

Discussion in 'C++' started by Der Andere, Jun 23, 2004.

  1. Der Andere

    Der Andere Guest

    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?
     
    Der Andere, Jun 23, 2004
    #1
    1. Advertising

  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
     
    Thomas Matthews, Jun 23, 2004
    #2
    1. Advertising

  3. "Der Andere" <> wrote in message
    news:cbc08b$doa$00$-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
     
    John Harrison, Jun 23, 2004
    #3
  4. Der Andere

    Allan Bruce Guest

    "Der Andere" <> wrote in message
    news:cbc08b$doa$00$-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
     
    Allan Bruce, Jun 23, 2004
    #4
  5. 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
     
    Karl Heinz Buchegger, Jun 23, 2004
    #5
  6. 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
     
    Gernot Frisch, Jun 23, 2004
    #6
  7. Der Andere

    Der Andere Guest

    > > 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
     
    Der Andere, Jun 23, 2004
    #7
  8. "Der Andere" <> wrote in message
    news:cbc32j$4t6$06$-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
     
    John Harrison, Jun 23, 2004
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Raymond Arthur St. Marie II of III

    very Very VERY dumb Question About The new Set( ) 's

    Raymond Arthur St. Marie II of III, Jul 23, 2003, in forum: Python
    Replies:
    4
    Views:
    518
    Raymond Hettinger
    Jul 27, 2003
  2. aghazalp

    very very basic question

    aghazalp, Apr 2, 2006, in forum: Python
    Replies:
    6
    Views:
    355
    aghazalp
    Apr 2, 2006
  3. shanx__=|;-

    very very very long integer

    shanx__=|;-, Oct 16, 2004, in forum: C Programming
    Replies:
    19
    Views:
    1,718
    Merrill & Michele
    Oct 19, 2004
  4. Peter

    Very very very basic question

    Peter, Feb 8, 2005, in forum: C Programming
    Replies:
    14
    Views:
    542
    Dave Thompson
    Feb 14, 2005
  5. mdh

    A very **very** basic question

    mdh, Sep 25, 2008, in forum: C Programming
    Replies:
    57
    Views:
    1,279
    August Karlstrom
    Sep 26, 2008
Loading...

Share This Page