Binary Association Problem

Discussion in 'C++' started by David, May 8, 2005.

  1. David

    David Guest

    Hi,
    I am trying to create a two way association between 2 objects. I
    intended to do it by having a pointer to each other but I am getting
    compilation errors.

    At the moment I can create both objects and use a pointer in 1 to
    access the other but as soon as I try to add a pointer in the other
    object it won't compile.

    This is simplified code of what I am trying to do. At the moment it is
    a one way association that works fine, the commented code is what I am
    wanting to do.

    ------------------------------------------------- obj1.h
    #ifndef _OBJ1_H_
    #define _OBJ1_H_

    #include "obj2.h"

    class obj1
    {
    public:
    obj1(int theNumber, obj2 * ptr);
    void output() const;
    private:
    int _num;
    obj2 * _obj2Ptr;
    };
    #endif
    -------------------------------------------------obj1.cpp
    #include "obj1.h"

    #include <iostream>
    using std::cout;
    using std::endl;

    obj1::eek:bj1(int theNumber, obj2 * ptr):
    _num(theNumber),
    _obj2Ptr(ptr)
    {
    }

    void obj1::eek:utput() const
    {
    cout << "obj1: " << _num << endl;
    cout << "obj1->2: ";
    _obj2Ptr->output() ;
    cout << endl;
    }
    -------------------------------------------------obj2.h
    #ifndef _OBJ2_H_
    #define _OBJ2_H_

    //#include "obj1.h" <-------as soon as I uncomment this line I get the
    compile errors below

    class obj2{

    public:
    obj2(int theNumber);
    void output() const;
    //void addPtr(obj1 * ptr);

    private:
    int _num;
    //obj1 * obj1Ptr;
    };

    #endif
    -------------------------------------------------obj2.cpp
    #include "obj2.h"

    #include <iostream>
    using std::cout;
    using std::endl;


    obj2::eek:bj2(int theNumber):
    _num(theNumber)
    {
    }

    //obj2::addPtr(obj1 * ptr)
    //{
    // _obj1Ptr = ptr;
    //}

    void obj2::eek:utput() const
    {
    cout << "obj2: " << _num << endl;
    //cout << "obj2->1:";
    //obj1Ptr->output();
    }
    ------------------------------------------------driver.cpp
    #include "obj1.h"
    #include "obj2.h"

    #include <iostream>
    using std::cout;
    using std::endl;

    int main(){

    obj2* two = new obj2 (5);
    two->output();

    obj1* one = new obj1(3, two);
    one->output();

    //two->addPtr(num2);
    return 0;
    }

    --------------------------------------------------errors
    obj1.h(13): error C2501: 'obj1::_obj2Ptr' : missing storage-class or
    type specifiers
    obj1.h(9): error C2062: type 'int' unexpected
    obj1.h(9): error C2238: unexpected token(s) preceding ';'
    obj1.h(13): error C2143: syntax error : missing ';' before '*'
    obj1.h(13): error C2501: 'obj1::eek:bj2' : missing storage-class or type
    specifiers

    Is there a way to have a binary association without a linkage class?

    Thanks in advance for any help!
    Dave
    David, May 8, 2005
    #1
    1. Advertising

  2. "David" <> a écrit ...

    Put:

    class Obj1;
    class Obj2;

    before the two declarations.

    One is suffisant, but two is better.

    One need prototyps for functions.
    I use to put prototyps for classes also.

    Etienne
    Etienne Rousee, May 8, 2005
    #2
    1. Advertising

  3. David

    David Guest

    Thanks!
    David, May 8, 2005
    #3
    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. MK
    Replies:
    2
    Views:
    2,471
  2. Mike
    Replies:
    6
    Views:
    1,094
  3. Alex Rast
    Replies:
    2
    Views:
    959
    Alex Rast
    Nov 10, 2004
  4. association problem

    , Jun 8, 2006, in forum: C++
    Replies:
    1
    Views:
    317
    Noah Roberts
    Jun 8, 2006
  5. Replies:
    1
    Views:
    668
    Tom Hawtin
    May 31, 2007
Loading...

Share This Page