Binary Association Problem

D

David

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
 
E

Etienne Rousee

"David" <[email protected]> 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
 

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,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top