static Data Members initialization

hi every body.........
There are two ways (I know) to set the [static] members
I tried both but none has worked
I'm gonna show you a Template of what I have(My code is a little bit biger , so a template would work)

when i used the [initialization out of the class] it shows me the next error msg:
"expected constructor, destructor, or type conversion before '=' token"

when i used the [static initializer function] it works fine till I use the class,
once i use the class it shows me an error msg and opens some code file from the libraries
[I'm gonna take a screen shot to show the error msg and the opened code file]

http://www.4shared.com/file/104225719/528f0d79/err_msg.html


here are the two ways I was talking about :
First One:

1
2
3
4
5
6
7
8
9
10
11
12
class class1{
    private:
    public:

    class1(){}

    static int DataMember;

    static void initFun()
        { DataMember = 10 ;  }
};


Second One:

1
2
3
4
5
6
7
8
9
10
class class2{
    private:
    public:

    class2(){}

    static int DataMember2;

};
class2::DataMember2 = 10 ;


any one knows what kinda trouble I'm facing
thanks in advance

Last edited on
First one is not valid.

Second one needs to look like this:

int class2::DataMember2 = 10 ;
Last edited on

Second one needs to look like this:

int class2::DataMember2 = 10 ;

I'm so sorry , i forgot the data type ...............
it's been a long time since I wrote in C++ (i use vb.net)

thanks for this I'll try it

but why is the first one not valid
sorry it still not working ......

I will have to cut a little snippet of my code to show you what exactly I'm tring to do.......

(I'll delete some code from it , to make it easier to see)

here is the code
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <iostream>
#include <windows.h>

using namespace std;


class point{
    private :

    public :
        int x , y;
    point (int _x , int _y):x(_x),y(_y){}
    point (){ x = 0 ; y = 0;}


     friend ostream& operator << (ostream& out ,point& P ){
        out << "x = " <<  P.x <<"\ty = " <<  P.y ;
        return out;
        }

     bool operator == (point& P ){

        if ( P.x ==x &&  P.y == y )
            return true;

        return false;
        }

// Here were some needed function for the project , but deleted to show you it easly
};
enum  Color
{
  WhiteSmoke = 8 ,
  Blue = 9,
  Green =10 ,
  Cyan = 11 ,
  Red =12 ,
  Pink = 13 ,
  Yellow = 14 ,
  White = 15

};
class Screen{
    private:

    public :
        static HANDLE hConsole ;
        Screen(){}

static void TextColor(Color c){
    SetConsoleTextAttribute(hConsole, c);
}

static void SetCursor(bool Visibile){

    CONSOLE_CURSOR_INFO cursor = {10, Visibile};
    SetConsoleCursorInfo(hConsole, &cursor);
}

static void gotoxy(int x,int y){
COORD coord ;
coord.X = x ;
coord.Y = y ;

SetConsoleCursorPosition(hConsole , coord) ;

}

static void gotoPoint(point P) {
    gotoxy(P.x, P.y);
}

static void PrintChar (int x , int y , char c ){
    gotoPoint (point (x,y));
    cout << c ;

    }

static void PrintChar (point P,  char c ){
    gotoPoint (P);
    cout << c ;
    }


};
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);

int main()
{
    Screen::PrintChar(5,5,'a');
    
    return 0;
}



any way (sory if it's late now) i didn't tell you I'm using Code::Blocks 8.02
You need to identify the class scope of the variable.
Screen::hConsole
Last edited on
thanks for reply ............

You need to identify the class scope of the variable.
Screen::hConsole


how ??????????

what could I do more than making it in the [public] section of the class and giving it the [static] access modifier ???????

do I have to do something to call (invoke - use , .....) it ??????? (I don't think so)

thanks in advance

};
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);


I'm so sorry Mr. kempofighter I didn't get it fast.................
so sorry

now I got the slip (long time didn't write C++)
it should have been

 
HANDLE Screen::hConsole = GetStdHandle(STD_OUTPUT_HANDLE);


[I think I have to quit doing things that are not in my interval ......... I always forget simple things and spend a long time searching for it ...........but sometimes one is enforced to do things that are ...............ok ... enough crying.... ]


thanks for every one shared me .......
Topic archived. No new replies allowed.