473,395 Members | 1,756 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,395 software developers and data experts.

invalid use of nonstatic member

6
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 23019
Banfa
9,065 Expert Mod 8TB
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
Is there a way to access m although?

Niels
Jul 19 '09 #3
Banfa
9,065 Expert Mod 8TB
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 Expert Mod 8TB
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
by: Jinesh | last post by:
I illustrate the compiler error I get using the following example. --------------------------------------------------------------- Class ClassName { private: static const int constVarName = 100;...
30
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
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...
3
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...
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...
2
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
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...
6
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...
2
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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,...
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...

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.