Error: Invalid use of non-static data member, in Nested Classes

To start with, I'm running windows 8 with Code::Blocks 12.11.

My main goal is to create a class named SCREEN with a member Array[][] and have this Array being modified and sending data to nested classes 'Trim' and 'ScreenClass'.

To make my goal better understood, this could be achieved in another way: same class 'SCREEN' containing DIRECLTY all the functions contained in 'Trim' and 'ScreenClass', without these two classes ever being created.
BUT in those too classes, 'Trim' and 'ScreenArray', I will input MUCH code, MUCH functions and I don't want them to be RAW in the 'SCREEN' class as finding each function then, would be a real trouble!! (I tried commenting like
//=========================================================
or 2 times the above, or even with asterisks (*) but when a lot of functions gather it's getting baaaaad.)


To start visualizing it, I want something like this:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
class SCREEN
{
private:
    char Array[160][160];//[x],[y]
    int CurrentWidth;
    
    class ScreenClass
    {
    private:
        void Initialize()
        {
            Array[x][y]=' '; //This refers to SCREEN::Array
        }
    public:
        bool Set(int x,int y)
        {
            CurrentWidth=10; //This refers to SCREEN::CurrentWidth
            Array[x][y]=' '; //This refers to SCREEN::Array
        }
        char Get(int x,int y) 
        {
            return Array[x][y]; //This refers to SCREEN::Array
        }

        ScreenClass(){Initialize();}

    };
    ScreenClass ScreenArray;


    class TrimClass 
    {//... Like ScreenClass. If I complete correctly ScreenClass, I will complete correctly this too 
        
        void Clean()
        {
            Array[1][1]=' '; //This refers to SCREEN::Array
        }
    };
    TrimClass Trim;

    // --other Functions--


public:
    
    void Clean (char Background=' ')
    {
        ScreenArray.Set(x,y);
    }
    void CleanTrim (char Background=' ') //NOTE: Here after trim class
    {
        Trim.Clean(Background);
    }
    void CleanAll (char Background=' ')
    {
        CleanTrim(Background);
        Clean(Background);
    }

    
    // --other Functions--

    SCREEN()
    {
        
    }
};



Which is basiclly and my code....

I want to use it in the end like:

1
2
3
4
5
6
7
8
9
int main()
{
    SCREEN Screen;
    Screen.Clean();
    //or
    Screen.Trim.Clean();
}


So I don't want to use any kind of static data..


I keep getting this error: invalid use of non-static data member 'SCREEN::X'
where X is Array, CurrentWidth and other data of SCREEN.



I tried googling something like 'nested classses error invalid use of non-static data member' among other but wthout resault.

I know that in order to use anything from ScreenClass I have to create an Object of ScreenClass (which can obviousy be seen in my code xD), but what I also learned is that a class declared and defined within an enclosing class, is not a UNIQUE property of the enclosing-class...

Therefore Trim (of type TrimClass) and ScreenArray (of type ScreenClass) coud have been created and outside of SCREEN (altough their class was defined within SCREEN) and that is why they dont know which object of SCREEN they must use the data from.

Well I want EACH object of type 'SCREEN' to have it's VERY OWN set of 'Trim' and 'ScreenArray' objects. To give an example:

1
2
3
4
class AClass
{
   int a;
}


In my exaple Each object of type AClass has it's very OWN integer 'a', therefore a coding line of a='something', would never send an error implying that 'a' doesn't know which object of AClass to edit 'a' from.... lol


Thanks in advance for any help :)
Extra info:

What I HAVE tried:

changing this
1
2
3
4
5
6
7
8
9
10
11
12
    class ScreenClass
    {
    private:
        void Initialize()
        {
            Array[x][y]=' '; //This refers to SCREEN::Array
        }
        //...
        ScreenClass(){Initialize();}

    };
    ScreenClass ScreenArray();


to this:

1
2
3
4
5
6
7
8
9
10
11
12
    class ScreenClass
    {
    private:
        void Initialize(SCREEN * const SCREENthis)
        {
            Array[x][y]=' '; //This refers to SCREEN::Array
        }
        //...
        ScreenClass(SCREEN * const SCREENthis){Initialize(SCREENthis);}

    };
    ScreenClass ScreenArray(this);


and all was fine.... except the last line 'ScreenClass ScreenArray(this);' where I got the error: expected identifier before 'this', and for the same line another error (or different aproach?): expected ',' or '...' before 'this'




P.S.:
If that will help at all, my problem IN ABSTRACT is (in my point of view)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class A
{
private/public/protected: (any of the three)
    //many data, from now on reffered to as A::Data.
private/public/protected:
    class B
    {
        void BFunction()
        {
            //Change A::data
        }
    }
    B b;
    
    void AFunction()
    {
        b.f();
    }
}

int main()
{
    A a;
    a.AFunction();

}
The member Trim of object Screen does not know that it is a member of that particular SCREEN object and therefore it does not know which Array to reference.

Each TrimClass object could have a pointer to its parent.
How? :D hahaha
I have no idea how to do this^^

Let's say I add one "SCREEN* ScreenPtr" within ScreenClass....
Do I initiate it? Do I give it a value? Where? With what code?

And thanks for telling me that. I didn't knew :)
Thanks a lot!
Last edited on
I didn't see your addendum while posting. You were almost there.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class A
{
  struct B
  {
    A * parent;

    B( A * p )
    : parent( p )
    {}

    void f()
    {
      cout << parent->data;
    }
  };

  int Data;
  B b;
    
public:
  A ()
  : Data( 42 ), b( this )
  {}

  void AFunction()
  {
    b.f();
  }
}
Last edited on
The Trim object is private, and therefore will not be able to be accessed from there(main).

Aceix.
Ok, REALY GOOD :D
but .. here are some questions: (xD)

In line 3: Why struct and not class?? O.o

In lines 7-8, 21-22: can I rewrite these this way-> "B(A*p) {parent = p;}", " A() {Data=42; b = this;}" ???? :D

It would help me A LOT (the way you wrote it gives me a headache to translate to something I understand :| .Nothing personal, Im talking about the so called ""Member initialization in constructors", if that is the correct name).

Another one. I have seen that before and I wοndered again about it,
in line 13: you reffer to "Data" while "Data" is FIRST time mentionedat line 17!! O.O
XD excuse my enthusiasm about this but I am new in programming and I don't quite understand this, not yet :d

Thanks for the code! :D
I'll go try it!

I'll come back with the resaults :D

Again, thank you!
Line 3: unimportant. Use what you need.

The constructors. See member initializer list http://en.cppreference.com/w/cpp/language/initializer_list

Line 13. My mistake (twice). We can write the implementation later:
1
2
3
4
5
6
7
8
9
10
class A {
  class B {
    void f();
  };
  int Data;
};

void A::B::f() {
  cout << parent->Data;
}
HOOOOOOOOOOOOOOOOOLY SH.......!!!!!! (You know the rest of the word! :D :D :D :D :D :D :D I'm gonna throw a party!! :D :D :D :D :D :D WOOOOOHOOOOOOO!!!!

I run a test on a minimalistic version of the program and I get no errors! :D

It must have worked!! (though I'm quite tired to try on the real program, it's 1:47 the late night right now here X| and I'm about to go to sleep)

I did ALL you said with an exception,
I didn't use struct but i used class (as mentioned in my question before)
and one HUGE note. When Initializing A like this: A(){b=this} I get an error.
When I do it like you said: A():b(this) {} I don't!! (*rubbing my eyes to see clearer).

I mean what the he*k!?!?!?! Isn't it the same?
Just a different methode for initializing?? O.o
Any hint for what should I read to understand the difference?
I read this topic* about it and thought/understood that this: A(){b=this} and A():b(this){} would be the same. *Topic: http://www.cplusplus.com/doc/tutorial/classes/

And I don't understand how the code in line 18 of your last code (as I see it 1:07 AM) works.... I mean it's constructor needs 1 parameter. An A* .. how can I say "B b;" ?
I guess that A():b(this) {} is involved.. But how? O.O

Again REEEEEALLY THANK YOU! :D You're the MAN! :)))))
@Aceix, thank you for your reply too :)
keskiverto was faster, problem solved! :D
Last edited on
I will see you tommorow. For today, have a good night (if it is night where you are right now) or a good day :)

...zZzZzZzZzZzZzZ (Necro is Fast asleep xD)
Hey, really sorry for not replying this long. I run into some troubles and I couldn't continue with the program.
Right now I just got curious if I shall change the way my program is structured.. xD

I mean, if I go the way I am already up to, I have this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
class SCREEN
{
        class ScreenClass
        {
                //...blah-blah...
                //....some more...

                ScreenClass(SCREEN* const ParentSCREEN)
        }
        ScreenClass ScreenArray;

        class TrimClass
        {
                class UpperTrim()
                {
                        //...blah-blah....
                        //this "blah-blah" above though requires a SCREEN and a ScreenClass....
                   Upper(SCREEN* const ParentSCREEN, ScreenClass* const ParentScreenClass)
                }
                UpperTrim Upper();
                class LowerTrim() //Almost same code with UpperTrim
                {} LowerTrim Lower;
                //.. Left, right and Corners classes as well.

                //And corners class is like THAT
                class CornersTrim
                {
                        class UpperRightCorner
                        {


                                UpperRight(ScREEN* const ParentSCREEN)
                        }
                        UpperRightCorner UpperRight;
                        //Same for Lower Right, UperLeft, LowerLeft ....                


                        CornersTrim(SREEN* const ParentSCREEN):UpperRight(ParentSCREEN),UpperLeft(...)//LowerRight and lowerleft
                        {}
                }
                CornersTrim Corners;


                TrimClass(SCREEN* const ParentSCREEN):Upper(ParentSCREEN,this), Lower(ParentSCREEN,this), Left(....), Right(....), Corenrs(....)
                {}
        }

        TrimClass Trim


        SCREEN():ScreenArray(this), Trim(this)
}



And I already guess that this option is not VIABLE! xD
I find this TOOOO much for something that simple.

Should I just imput classes like Trim and ScreenClass in Header files on top of the class SCREEN and use the same methode (all those pointers), or is there a better way?
example:
1
2
3
4
5
6
7
8
#include "Trim.h"
#include "ScreenClass.h"

SCREEN
{
    TrimClass Trim
    //etc
}

The above would put less "strein", and would require less effort to read for the SCREEN class. ( I think..)




I thought of doing this: Instead of creating classes for manipulating the Trim and the Array in my SCREEN class, to just make simple fuctions, and to deal with the large amount of code in a single class, to include in headers the functions of my SCREEN class categorized.

In example:

1
2
3
4
5
6
SCREEN
{
    #include "TrimFunctions.h"
    #include "ArrayFunctions.h"

}


But aren't wesupposed to only include DECLARATION of CLASSES in headers? O.o

I'm pretty stuck.



Hmmm, another though:

Since my SCREEN is a suplementary class, and not to work on it's own (well it can, but it is not intended. I want it for other programs) , should I create an easy to see and maintain interface of it at a header SCREEN.h and then just define the functions at a SCREEN.cpp?

Help? :D
Serioucly, any help apreciated!
I'm new at programming and I can't say I found my way yet
Last edited on
Topic archived. No new replies allowed.