473,465 Members | 1,801 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

two classes #including each other

hi, i imagine we've all seen this one posted before but i can't get
any solutions to work for me.. for example:

i have two classes MyClass and Shape. MyClass creates Shapes (and
derived classes from Shape) and it also wants to pass a pointer to
itself to a member function of Shape so that it can access MyClass'
members. Pseudo code example:

class MyClass {

void MyFunction{
aShape->DoSomethingToShape(this)
}

private:

Shape* aShape;
}

/////////////

class MyClass; // forward declaration

class Shape { // is a base class

void DoSomethingToShape(MyClass* aClass) {
aClass->some_member; // ** ERROR invalid use of undefined type
'struct MyClass' **
}

I have simplified greatly (possibly too much to make proper sense).
Really i'm trying to avoid passing 10-20 references to MyClass members
because it feels a bit clumsy. I just want to pass a pointer to a
MyClass so that Shape can access what it wants.

Is there a better way?

thanks, stefven.

Aug 24 '07 #1
5 1655
On Aug 24, 9:47 pm, stefven blonqhern <robo_cree...@yahoo.co.uk>
wrote:
hi, i imagine we've all seen this one posted before but i can't get
any solutions to work for me.. for example:

i have two classes MyClass and Shape. MyClass creates Shapes (and
derived classes from Shape) and it also wants to pass a pointer to
itself to a member function of Shape so that it can access MyClass'
members. Pseudo code example:

class MyClass {

void MyFunction{
aShape->DoSomethingToShape(this)

}

private:

Shape* aShape;

}

/////////////

class MyClass; // forward declaration

class Shape { // is a base class

void DoSomethingToShape(MyClass* aClass) {
aClass->some_member; // ** ERROR invalid use of undefined type
myClass needs to be defined before its members are used.
'struct MyClass' **

}

I have simplified greatly (possibly too much to make proper sense).
Really i'm trying to avoid passing 10-20 references to MyClass members
because it feels a bit clumsy. I just want to pass a pointer to a
MyClass so that Shape can access what it wants.
You can do something like this: (just an example):

//Myclass.h
class Myclass
{
public:
void foo();
};
//Myclass.cpp
#include "Myclass.h"
void Myclass::foo()
{
...
}
//shape.h
class Myclass;
class Shape
{
public:
void DoSomethingToShape(Myclass*);
};
//shape.cpp
#include "shape.h"
#include "myclass.h"

void Shape::DoSomethingToShape(Myclass* aClass)
{
aClass->foo();
}
-N

Aug 24 '07 #2
stefven blonqhern wrote:
hi, i imagine we've all seen this one posted before but i can't get
any solutions to work for me.. for example:

i have two classes MyClass and Shape. MyClass creates Shapes (and
derived classes from Shape) and it also wants to pass a pointer to
itself to a member function of Shape so that it can access MyClass'
members. Pseudo code example:

class MyClass {

void MyFunction{
aShape->DoSomethingToShape(this)
}

private:

Shape* aShape;
}

/////////////

class MyClass; // forward declaration

class Shape { // is a base class

void DoSomethingToShape(MyClass* aClass) {
aClass->some_member; // ** ERROR invalid use of undefined type
'struct MyClass' **
}
Pull this function's definition out of the class 'Shape' and
#include <MyClass.hright in front of it (if you need this to be
in the header). Or just place it in a separate translation unit.
I have simplified greatly (possibly too much to make proper sense).
Really i'm trying to avoid passing 10-20 references to MyClass members
because it feels a bit clumsy. I just want to pass a pointer to a
MyClass so that Shape can access what it wants.

Is there a better way?
Usually, yes. The Java habit of having all functions implemented in
the class definition is not the best to follow when C++ is concerned.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 24 '07 #3
okay, thanks for the replies.. naturally i forgot to mention that i'm
using public inheritance. Shape being the base class and all member
functions are virtual.

looks like i have some thinking to do, thanks,
stefven.

Aug 24 '07 #4
Is there a better way?
>
Usually, yes. The Java habit of having all functions implemented in
the class definition is not the best to follow when C++ is concerned.
so really i should be separating the interface and the
implementation. Java? never heard of it!

Stefven

Aug 24 '07 #5
stefven blonqhern wrote:
>>Is there a better way?

Usually, yes. The Java habit of having all functions implemented in
the class definition is not the best to follow when C++ is concerned.

so really i should be separating the interface and the
implementation. Java? never heard of it!
Interface and implemenatation are concepts, they are separate by
nature. You should be separating declarations and definitions.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 24 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Phil Powell | last post by:
Where can I find an online PHP form validator script library to use? I have tried hacking the one here at work for weeks now and it's getting more and more impossible to customize, especially now...
0
by: Rare Book School | last post by:
RARE BOOK SCHOOL 2005 Rare Book School is pleased to announce its schedule of courses for 2005, including sessions at the University of Virginia, the Walters Art Museum/Johns Hopkins University...
1
by: Bob Rock | last post by:
Hello, in the last few days I've made my first few attempts at creating mixed C++ managed-unmanaged assemblies and looking aftwerwards with ILDASM at what is visible in those assemblies from a...
11
by: C# Learner | last post by:
Is it not possible to declare a nested class in a seperate file from its "parent" class -- i.e. in a similar way to the idea of spreading namespaces over more than one file?
11
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you...
4
by: Chris F Clark | last post by:
Please excuse the length of this post, I am unfortunately long-winded, and don't know how to make my postings more brief. I have a C++ class library (and application generator, called Yacc++(r)...
5
by: Kyote | last post by:
Sorry, but I have no idea how to phrase the subject better than that. I've come across this a few different times and decided to ask in case there's a way to do it. It would simplify things a bit...
86
by: jopperdepopper | last post by:
Hi, finally giving php 5 a go, and going over the new approach to classes. Can someone clarify the public, private and protected to me? I quote the php manual: "The visibility of a property or...
12
by: raylopez99 | last post by:
Keywords: scope resolution, passing classes between parent and child forms, parameter constructor method, normal constructor, default constructor, forward reference, sharing classes between forms....
1
by: puzzlecracker | last post by:
Hello Group, Say I have classes A and B and the both need to maintain a pointer/ reference to each for various reasons, say optimization. And there are other classes in the project use these...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.