473,756 Members | 1,841 Online
Bytes | Software Development & Data Engineering Community
+ 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->DoSomethingToS hape(this)
}

private:

Shape* aShape;
}

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

class MyClass; // forward declaration

class Shape { // is a base class

void DoSomethingToSh ape(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 1676
On Aug 24, 9:47 pm, stefven blonqhern <robo_cree...@y ahoo.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->DoSomethingToS hape(this)

}

private:

Shape* aShape;

}

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

class MyClass; // forward declaration

class Shape { // is a base class

void DoSomethingToSh ape(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 DoSomethingToSh ape(Myclass*);
};
//shape.cpp
#include "shape.h"
#include "myclass.h"

void Shape::DoSometh ingToShape(Mycl ass* 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->DoSomethingToS hape(this)
}

private:

Shape* aShape;
}

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

class MyClass; // forward declaration

class Shape { // is a base class

void DoSomethingToSh ape(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.hrig ht 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
8294
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 with form elements that turn out to be arrays that have to be compared with one another! I have one form element, languages, a checkbox group. Beside each checkbox is a dropdown, proficiency (which will become proficiency alongside languages)....
0
1743
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 in Baltimore, and the Freer/Sackler Galleries in Washington, DC. Please visit our web site for a complete brochure, expanded course descriptions, and application forms: <www.rarebookschool.org>
1
741
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 managed point-of-view I've noticed that: 1) for each managed and unmanaged C function (not C++ classes) I get a public managed static method (defined on a 'Global Functions' class) in the generated assembly with an export name of the form...
11
2726
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
3843
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 experts. I would like to produce Javascript classes that can be "subclassed" with certain behaviors defined at subclass time. There are plenty of ways to do this through prototyping and other techniques, but these behaviors need to be static and...
4
2133
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) and the Language Objects Library) that I have converted over to C#. It works okay. However, in the C# version, one has to build the class library into the generated application, because I haven't structured this one thing right. I would like to...
5
1309
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 and I figured if anyone knows, more than likely that someone is here. I have a class for storing filenames and it's path along with a little more info. Here it is Public Class myFiles Public oldFileName As String
86
4641
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 method can be defined by prefixing the declaration with the keywords: public, protected or private. Public declared items can be accessed everywhere."
12
11107
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. Here is a newbie mistake that I found myself doing (as a newbie), and that even a master programmer, the guru of this forum, Jon Skeet, missed! (He knows this I'm sure, but just didn't think this was my problem; LOL, I am needling him) If...
1
1478
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 two classes, among others. What is the best design for such architecture? I saw errors by explicitly including headers of both classes. Also,
0
9455
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9271
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10031
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9869
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9708
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7242
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5140
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5302
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2665
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.