class inheritance and compilation problem. Linux RH6.2. g++

  • Follow


Halo
I have a problem with the following program:

#include <stdlib.h>

struct Base {
  int a;
  Base() {
    a=0;
  }
  Base(int a): a(a) {};
};

class InherInher;

struct Inher: public Base {
  Inher(int a): Base(a) {};


// this is the problem
  struct Base* getObj() {
    Base *ptr = new InherInher( a );
    return  ptr;
  }
};

struct InherInher: public Inher {
  InherInher(int a): Inher( a ) {};
};

main() {
}

Have You got any ideas how to make it work

Greetings
Maciej



0
Reply pikpus (7) 7/9/2003 2:06:36 PM

"Maciej Kwapulinski" <pikpus@wp.pl> wrote...
> I have a problem with the following program:
>
> #include <stdlib.h>

I don't think you use anything from this header here.

>
> struct Base {
>   int a;
>   Base() {
>     a=0;
>   }
>   Base(int a): a(a) {};


Lose the trailing semicolon.

> };
>
> class InherInher;
>
> struct Inher: public Base {
>   Inher(int a): Base(a) {};


Lose the trailing semicolon.

>
>
> // this is the problem
>   struct Base* getObj() {
>     Base *ptr = new InherInher( a );
>     return  ptr;
>   }

Move it down after the 'InherInher' definition and declare it
inline.  Leave only a declaration here:

    Base* getObj();

> };
>
> struct InherInher: public Inher {
>   InherInher(int a): Inher( a ) {};

Lose the trailing semicolon.

> };

Here is where you move the member definition:

  inline Base* Inher::getObj() {
    Base *ptr = new InherInher( a );
    return  ptr;
  }

>
> main() {

int main() {

> }
>
> Have You got any ideas how to make it work

See above

Victor


0
Reply v.Abazarov1 (626) 7/9/2003 2:26:01 PM



Maciej Kwapulinski wrote:
> 
[snip]
> 
> Have You got any ideas how to make it work
> 

There is no other way then to move the definition
of function Inher::getObj() past the declaration
of InherInher

....

struct Inher: public Base {
   Inher(int a): Base(a) {};
   struct Base* getObj();
};
 
struct InherInher: public Inher {
  InherInher(int a): Inher( a ) {};
};

struct Base* Inher::getObj()
{
  Base *ptr = new InherInher( a );
  return  ptr;
}

int main()
{
}

-- 
Karl Heinz Buchegger
kbuchegg@gascad.at
0
Reply kbuchegg (2095) 7/9/2003 2:26:28 PM

That's it !!
Great thanks


"Victor Bazarov" <v.Abazarov@attAbi.com> wrote in message
news:vgo9gcfg62271e@corp.supernews.com...
> "Maciej Kwapulinski" <pikpus@wp.pl> wrote...
> > I have a problem with the following program:
> >
> > #include <stdlib.h>
>
> I don't think you use anything from this header here.
>
> >
> > struct Base {
> >   int a;
> >   Base() {
> >     a=0;
> >   }
> >   Base(int a): a(a) {};
>
>
> Lose the trailing semicolon.
>
> > };
> >
> > class InherInher;
> >
> > struct Inher: public Base {
> >   Inher(int a): Base(a) {};
>
>
> Lose the trailing semicolon.
>
> >
> >
> > // this is the problem
> >   struct Base* getObj() {
> >     Base *ptr = new InherInher( a );
> >     return  ptr;
> >   }
>
> Move it down after the 'InherInher' definition and declare it
> inline.  Leave only a declaration here:
>
>     Base* getObj();
>
> > };
> >
> > struct InherInher: public Inher {
> >   InherInher(int a): Inher( a ) {};
>
> Lose the trailing semicolon.
>
> > };
>
> Here is where you move the member definition:
>
>   inline Base* Inher::getObj() {
>     Base *ptr = new InherInher( a );
>     return  ptr;
>   }
>
> >
> > main() {
>
> int main() {
>
> > }
> >
> > Have You got any ideas how to make it work
>
> See above
>
> Victor
>
>


0
Reply pikpus (7) 7/9/2003 4:26:50 PM

3 Replies
41 Views

(page loaded in 0.086 seconds)

Similiar Articles:




7/24/2012 5:24:16 AM


Reply: