473,396 Members | 1,997 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

error: `struct A' is inaccessible


Staring with g++ 3.4, this code results in an error

----------------------------------

struct A {
};

class B : private A {
};

class C : public B {
C(A& a);
};
---------------------------------

b.cc:2: error: `struct A' is inaccessible
b.cc:9: error: within this context

This looks wrong to me. You can't access B as an A, but why C can't use
the public A independently? what do other
compliers say?

(Think of the plausible usage
============
struct A {
};

class B : private A {
public:
B(A&) {}
};

class C : public B {
C(A& a) : B(a) {}
};
============
)
Can someone explain this?

Thanks, Joseph

Nov 11 '06 #1
1 10057


jh****@gmail.com wrote:
Staring with g++ 3.4, this code results in an error

----------------------------------

struct A {
};

class B : private A {
};

class C : public B {
C(A& a);
};
---------------------------------

b.cc:2: error: `struct A' is inaccessible
b.cc:9: error: within this context

This looks wrong to me. You can't access B as an A, but why C can't use
the public A independently? what do other
compliers say?
This is called "class name injection". Every time you define class 'A',
name 'A' is implicitly inserted ("injected") into the scope of 'A' as a
public member (see 9/3). In is loosely equivalent to

class A {
public:
typedef ::A A; // (informally, just a sketch)
...
};

Now, every time you refer to unqualified 'A' inside 'A' or any class
derived from 'A' directly or indirectly, name lookup will find 'A::A',
not the global 'A'.

(Note: an explicit attempt to use 'A::A' in the program will be treated
as a reference to the constructor, not to the injected class name).

That's exactly what happens in your case. When you refer to 'A' in
constructor declaration of 'C', name lookup finds the injected 'A::A',
which is inaccessible in 'C'.

You can work around this by using qualified name '::A'

class C : public B {
C(::A& a);
};

--
Best regards,
Andrey Tarasevich

Nov 11 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: muser | last post by:
Can anyone run this program through their compiler or if they can see a logical error please point it out. I have my tutor working on it at the moment but I would rather a less ambigious response...
0
by: Uzytkownik | last post by:
I don't know where and why the error is. Klasa.h: class Klasa1 { protected: int A, D, M, T; public: Klasa1(int a = -1, int d = -1, int m = -1, int t = -1); Klasa1(Klasa1&);
4
by: Rustam Asgarov | last post by:
I have a structure like this : public struct index_struct { static char IP = new char ; int port; static char FileN = new char ; int ID; int Bnum; long m_DataSize; int Version;
1
by: Larry | last post by:
I have a VB background and am developing a new windows app in Csharp. I'm getting the error. 'inaccessible due to its protection level' I've added a TextBox1 and a Button1 to a form. I...
6
by: Bill Rubin | last post by:
The following code snippet shows that VC++ 7.1 correctly compiles a static member function invocation from an Unrelated class, since this static member function is public. I expected to compile the...
7
by: Fir5tSight | last post by:
Hi All, I used the following code in C#: using System.Drawing; //blah blah blah Bitmap bmp = new Bitmap();
4
by: B. | last post by:
Hi I have two projects, one written in MC++, and the other in C#. In MC++ header file, I have: namespace Domain { namespace Subdomain { namespace ManagedCPP { public class AClass
6
by: milund | last post by:
I have a "funny" after upgrading to .NET2.0. The following code is placed inside an unsafe method in assembly "A" System.Object myIUnknownObject =...
0
by: Shane | last post by:
(reposted from comp.sys.hp.hpux) The following code compiles fine on A5.52 and other compilers, however aCC 6.15 generates an error: #include <vector> using namespace std; class ClassA :...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.