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

  • Thread starter Maciej Kwapulinski
  • Start date
M

Maciej Kwapulinski

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
 
V

Victor Bazarov

Maciej Kwapulinski said:
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;
}

int main() {
}

Have You got any ideas how to make it work

See above

Victor
 
K

Karl Heinz Buchegger

Maciej said:
[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()
{
}
 
M

Maciej Kwapulinski

That's it !!
Great thanks


Victor Bazarov said:
I don't think you use anything from this header here.



Lose the trailing semicolon.



Lose the trailing semicolon.


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

Base* getObj();


Lose the trailing semicolon.


Here is where you move the member definition:

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


int main() {


See above

Victor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top