473,804 Members | 4,005 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

invalid use of nonstatic member

6 New Member
Hi!

Why does'nt the following work?

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2.  
  3. class X
  4. {
  5. public:
  6.     X()
  7.      : m(0)
  8.     { }
  9.  
  10.     virtual ~X()
  11.     { }
  12.  
  13. protected:
  14.     int m;
  15. };
  16.  
  17. class Y : public X
  18. {
  19. private:
  20.     class Z 
  21.     {
  22.         void do_something() 
  23.         {
  24.             std::cout << m << std::endl;
  25.         }
  26.     };
  27. };
I get:
Expand|Select|Wrap|Line Numbers
  1. error: invalid use of nonstatic data member ‘X::m’ 
Thanks!

Niels
Jul 19 '09 #1
4 23048
Banfa
9,065 Recognized Expert Moderator Expert
m is a non static member of X which is the inherited by Y. You try to us it in Z but Z is a separate class, it's only link with Y is that it is a nested definition.

That means that when Z::do_something is running it is not in the presence of a this pointer that points to an instance of Y. It is in the presence of a this pointer pointing to the instance of Z. So there is no conext present through which to access m.

Like the error message says you have tried to use m in an invalid context.
Jul 19 '09 #2
nielsp
6 New Member
Is there a way to access m although?

Niels
Jul 19 '09 #3
Banfa
9,065 Recognized Expert Moderator Expert
Only if you have a reference to, pointer to or instance of Y (or X).

m can't be accessed in Z because the existence of an object of type Z does not imply the existence of an object of type Y.

There is no m to access in Z::do_something
Jul 20 '09 #4
weaknessforcats
9,208 Recognized Expert Moderator Expert
You will need to write an accessor function for Y to return m and then call that accessor in the Z member function.
Jul 27 '09 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

8
2972
by: Jinesh | last post by:
I illustrate the compiler error I get using the following example. --------------------------------------------------------------- Class ClassName { private: static const int constVarName = 100; void functionName(int parameterName) }; void ClassName::functionName(int parameterName=constVarName)
30
3501
by: Joost Ronkes Agerbeek | last post by:
Why is it allowed in C++ to call a static member function of an object through an instance of that object? Is it just convenience? tia, Joost Ronkes Agerbeek
2
2315
by: Steven T. Hatton | last post by:
I find the surprising. If I derive Rectangle from Point, I can access the members of Point inherited by Rectangle _IF_ they are actually members of a Rectangle. If I have a member of type Point in Rectangle, the compiler tells me Point::x is protected. I would have expected Rectangle to see the protected members of any Point. Compiling the following code give me this error: g++ -o rectangle main.cc main.cc: In member function `size_t...
3
5141
by: Clay_Culver | last post by:
I have this code: typedef BraidedNode* (BraidedNode::*NodeGet)() const; NodeGet test() { return BraidedNode::getNextID; } This code compiles under MSVC 7.1, but g++ (GCC 3.4.4) will not compile
6
4413
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 same invocation from a DistantlyRelated class. What actually happened was that the compiler produced: error C2247: 'A::function' not accessible because 'CloselyRelated' uses 'private' to inherit from 'A' I'm guessing that the above compiler...
2
17392
by: Gabrielle A. Grün | last post by:
Hi All, Does anyone know a way around the illegal reference of a non-static member? Thanks. iNHERITANCE HIERACY
22
2784
by: ypjofficial | last post by:
Is there any possibility of invoking the member functions of a class without creating an object (or even a pointer to ) of that class. eg. #include <iostream.h> class test { public: void fun() {
6
2217
by: fl | last post by:
Hi, There is a question about nonstatic member. C++ primer says: A nonstatic member is restricted to being declared as a pointer or reference to an object of its class. It only gives an example of pointer *b. class Bar { public: private:
2
6607
by: Noor Alsawadi | last post by:
Hi, i am a student, i have knowledge in java but im still a beginner in C++ . i am doing a simple inheritance program, where my base class has a header "clockType.h" and an implementation file "clock imp.cpp" my derived class header is "extenclock" and the implementation file is "extenimp" My problem is that when i want to define my setTime() function like this: void extenClock::setTime(int h, int m, int s, string t) ...
0
9706
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10575
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10330
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9144
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7616
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6851
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5520
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4297
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3816
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.