[comp.lang.c++] Is it just me? Problem, need help...

grimlok@hubcap.clemson.edu (Mike Percy) (03/02/91)

This is a simplification of a real-code problem.
Any help would be appreciated!

Turbo C++  Version 1.01 Copyright (c) 1990 Borland International
temp.cpp:
Error temp.cpp 10: 'func' is not a member of 'bar' in
function foo::barfunc()
*** 1 errors in Compile ***
--------------------------------------------------------------------
class bar;

class foo {
  bar *barptr;
public:
  void barfunc() { barptr->func(); }
  void func();
};


class bar {
  foo *fooptr;
public:
  void foofunc() { fooptr->func(); }
  void func();
};
--------------------------------------------------------------------
 
Is there a way to declare this so that everything works?

"I don't know about your brain, but mine is really...bossy."
Mike Percy                    grimlok@hubcap.clemson.edu
ISD, Clemson University       mspercy@clemson.BITNET
(803)656-3780                 mspercy@clemson.clemson.edu

ahodgson@athena.mit.edu (Antony Hodgson) (03/02/91)

In article <13358@hubcap.clemson.edu> grimlok@hubcap.clemson.edu (Mike Percy) writes:
>This is a simplification of a real-code problem.
>Any help would be appreciated!
>
>Turbo C++  Version 1.01 Copyright (c) 1990 Borland International
>temp.cpp:
>Error temp.cpp 10: 'func' is not a member of 'bar' in
>function foo::barfunc()
>*** 1 errors in Compile ***
>--------------------------------------------------------------------
>class bar;
>
>class foo {
>  bar *barptr;
>public:
>  void barfunc() { barptr->func(); }
>  void func();
>};
>
>
>class bar {
>  foo *fooptr;
>public:
>  void foofunc() { fooptr->func(); }
>  void func();
>};

The problem is that at the time the compiler reaches the definition of
foo::barfunc(), it doesn't know what members bar has.  One solution is
to not define the code for foo::barfunc() here, but wait until after
class bar has been defined.  If you want barfunc to be inline, use the
inline qualifier when you define it later.

An alternative is to change your design a bit.  If both foo and bar have
func() member functions which mean similar things, perhaps both of them
should be derived from a common base class which has a virtual func();
both foo and bar will then inherit this routine and foo::barfunc() will
know about it.

Tony Hodgson
ahodgson@hstbme.mit.edu

grimlok@hubcap.clemson.edu (Mike Percy) (03/02/91)

grimlok@hubcap.clemson.edu (Mike Percy) writes:

>This is a simplification of a real-code problem.
>Any help would be appreciated!

>Turbo C++  Version 1.01 Copyright (c) 1990 Borland International
>temp.cpp:
>Error temp.cpp 10: 'func' is not a member of 'bar' in
>function foo::barfunc()
>*** 1 errors in Compile ***
>--------------------------------------------------------------------
>class bar;

>class foo {
>  bar *barptr;
>public:
>  void barfunc() { barptr->func(); }
>  void func();
>};


>class bar {
>  foo *fooptr;
>public:
>  void foofunc() { fooptr->func(); }
>  void func();
>};
>--------------------------------------------------------------------
> 
>Is there a way to declare this so that everything works?
 
Thanks to those who helped. (Sound of forehead-smacking!)
My solution was to remove the function declarations for barfunc() and
foofunc() and insert (after both classes are defined!)
 
inline void foo::barfunc()
{
  barptr->func();
}
 
inline void bar::foofunc()
{
  fooptr->func();
}
 
Taa-daa!
 
"I don't know about your brain, but mine is really...bossy."
Mike Percy                    grimlok@hubcap.clemson.edu
ISD, Clemson University       mspercy@clemson.BITNET
(803)656-3780                 mspercy@clemson.clemson.edu