Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Binary Association Problem

Reply
Thread Tools

Binary Association Problem

 
 
David
Guest
Posts: n/a
 
      05-08-2005
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

 
Reply With Quote
 
 
 
 
Etienne Rousee
Guest
Posts: n/a
 
      05-08-2005

"David" <(E-Mail Removed)> 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


 
Reply With Quote
 
 
 
 
David
Guest
Posts: n/a
 
      05-08-2005
Thanks!

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Multiple java applications file association problem in one JNLP? yp.yean@gmail.com Java 1 05-31-2007 11:23 AM
Re: File association problem WMP 11 Pennywise@DerryMaine.Gov Computer Support 6 10-19-2006 03:30 AM
Re: File association problem WMP 11 The Cross-eyed Barber Computer Support 0 10-15-2006 07:09 PM
association problem omariqbalnaru@inbox.com C++ 1 06-08-2006 03:54 PM
File association problem? flyernzl Computer Support 2 10-12-2004 06:50 PM



Advertisments