inaccessible base despite public inheritance

S

Stefan Weber

Hi,

I have the following header file (named test.h):

class Foo
{
public:
Foo(void) {}
virtual ~Foo(void) {}
};

class Bar : public Foo
{
public:
Bar(void) {}
virtual ~Bar(void) {}
};

with a very simple cpp file:

#include "test.h"

int main() {
Foo* f = new Bar();
}

This compiles with Visual Studio 2005, but with g++ 4.1.1 I get an
error:

bash-3.00$ test.cpp:4: error: Foo is an inaccessible base of Bar

Acutally, I know that this error message occurs when doing something
like "class Bar : private Foo" (i.e. inherit the code but withouth
polymorphism). But I obviously declared to use public inheritance.
Does anybody see the problem?

Thanks,

Stefan
 
V

Victor Bazarov

Stefan said:
I have the following header file (named test.h):

class Foo
{
public:
Foo(void) {}
virtual ~Foo(void) {}
};

class Bar : public Foo
{
public:
Bar(void) {}
virtual ~Bar(void) {}
};

with a very simple cpp file:

#include "test.h"

int main() {
Foo* f = new Bar();
}

This compiles with Visual Studio 2005, but with g++ 4.1.1 I get an
error:

bash-3.00$ test.cpp:4: error: Foo is an inaccessible base of Bar

Acutally, I know that this error message occurs when doing something
like "class Bar : private Foo" (i.e. inherit the code but withouth
polymorphism). But I obviously declared to use public inheritance.
Does anybody see the problem?

The only /potential/ problem is the name of the header file.

Put the code you posted in the same cpp file and compile it again
(comment out 'with a very...' and the '#include'). If it compiles,
the problem isn't in the code. If it doesn't, you're SOL. Get
a better compiler.

V
 
H

Howard

Stefan Weber said:
Hi,

I have the following header file (named test.h):

class Foo
{
public:
Foo(void) {}
virtual ~Foo(void) {}
};

class Bar : public Foo
{
public:
Bar(void) {}
virtual ~Bar(void) {}
};

with a very simple cpp file:

#include "test.h"

int main() {
Foo* f = new Bar();
}

This compiles with Visual Studio 2005, but with g++ 4.1.1 I get an
error:

bash-3.00$ test.cpp:4: error: Foo is an inaccessible base of Bar

Acutally, I know that this error message occurs when doing something
like "class Bar : private Foo" (i.e. inherit the code but withouth
polymorphism). But I obviously declared to use public inheritance.
Does anybody see the problem?

The only thing I see unusual is the use of void as a parameter type. In
C++, don't use "(void)" when there's no parameter, just use "()". I don't
know if it's an error, but it's certainly not the norm.

-Howard
 
V

Victor Bazarov

Howard said:
[..]
The only thing I see unusual is the use of void as a parameter type.

Unfortunately, it's not as unusual as it ought to be.
In C++, don't use "(void)" when there's no parameter, just use "()".

I agree, it's a better style IMO, but they are definitely the same
AFA function declarations are concerned.
I don't know if it's an error, but it's certainly not the norm.

It's perfectly fine from the syntactic point of view.

V
 
F

Fei Liu

Stefan said:
Hi,

I have the following header file (named test.h):

class Foo
{
public:
Foo(void) {}
virtual ~Foo(void) {}
};

class Bar : public Foo
{
public:
Bar(void) {}
virtual ~Bar(void) {}
};

with a very simple cpp file:

#include "test.h"

int main() {
Foo* f = new Bar();
}

This compiles with Visual Studio 2005, but with g++ 4.1.1 I get an
error:

bash-3.00$ test.cpp:4: error: Foo is an inaccessible base of Bar

Acutally, I know that this error message occurs when doing something
like "class Bar : private Foo" (i.e. inherit the code but withouth
polymorphism). But I obviously declared to use public inheritance.
Does anybody see the problem?

Thanks,

Stefan

Are you sure this is the complete story? How exactly do you compile your
files with gcc 4.1.1?

F
 
G

Guillaume

Your code looks fine, are you sure there's no other test.h file in the
way ?
Acutally, I know that this error message occurs when doing something
like "class Bar : private Foo" (i.e. inherit the code but withouth
polymorphism).

Deriving a class with the keyword "private" doesn't imply anything
about polymorphism. It just means that private members of the base
class will not be visible from the derived class.


- Guillaume.
 
G

Guillaume

Your code looks fine, are you sure there's no other test.h file in the
way ?
Acutally, I know that this error message occurs when doing something
like "class Bar : private Foo" (i.e. inherit the code but withouth
polymorphism).

Deriving a class with the keyword "private" doesn't imply anything
about polymorphism. It just means that private members of the base
class will not be visible from the derived class.


- Guillaume.
 
G

Guillaume

Your code looks fine, are you sure there's no other test.h file in the
way ?
Acutally, I know that this error message occurs when doing something
like "class Bar : private Foo" (i.e. inherit the code but withouth
polymorphism).

Deriving a class with the keyword "private" doesn't imply anything
about polymorphism. It just means that private members of the base
class will not be visible from the derived class.


- Guillaume.
 
S

Stefan Weber

The only /potential/ problem is the name of the header file.
Put the code you posted in the same cpp file and compile it again
(comment out 'with a very...' and the '#include'). If it compiles,
the problem isn't in the code. If it doesn't, you're SOL. Get
a better compiler.

Thanks for the hint, this helped :) So the actual code obviously was
correct.
 

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

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,831
Latest member
RusselWill

Latest Threads

Top