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:bj1(int theNumber, obj2 * ptr):
_num(theNumber),
_obj2Ptr(ptr)
{
}
void obj1: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:bj2(int theNumber):
_num(theNumber)
{
}
//obj2::addPtr(obj1 * ptr)
//{
// _obj1Ptr = ptr;
//}
void obj2: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: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
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:bj1(int theNumber, obj2 * ptr):
_num(theNumber),
_obj2Ptr(ptr)
{
}
void obj1: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:bj2(int theNumber):
_num(theNumber)
{
}
//obj2::addPtr(obj1 * ptr)
//{
// _obj1Ptr = ptr;
//}
void obj2: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: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