building my arrays class

Pages: 12
i'm trying build my own array class(i need test 1 thing with it about the vectors):
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
template <typename tmArray>
class arrays
{
    private:
        int arraysize{0};
        tmArray value[];
        bool blnResized{false};

    public:
        arrays(int arrayssize=0)
        {
            if(arrayssize!=0)
            {
                blnResized=true;
                value=(tmArray ) malloc(arrayssize*sizeof(int));
            }
        }

        void Resize(int intResize)
        {
            blnResized=true;
            value =(tmArray ) malloc(intResize*sizeof(int));
        }

        int Size()
        {
            return sizeof(value)/sizeof(tmArray);
        }

        ~arrays()
        {
            if(blnResized==true)
                free(value);
        }
};

heres how i use it:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
arrays<button> btnarray;

int WinMain()
{
    //OutputDebugString("hello world");
    //int arraysize=ArraySize(btnarray, button);//geting the array size
    btnarray.Resize(8);
    int arraysize=btnarray.Size();
    int i=0;
    for (i; i<arraysize; i++)
    {
        int inttop=150 + (i*50);

        btnarray[i].setTop(inttop);
        btnarray[i].setLeft(0);
        btnarray[i].setText(to_string(i));
        btnarray[i].MouseClick=[i]()
        {
            MessageBox(btnarray[i].getText());
        };
        debugtext((string)to_string(inttop) + "\t" + to_string(i));
    }

i get several errors on several lines:
on:

"no match for 'operator[]' (operand types are 'arrays<button>' and 'int')"
maybe i forget overloading the operator[]. can anyone advice me?
maybe i forget overloading the operator[].
You did. There is no such overload anywhere in your code.

Also your size function is wrong. It will not actually calculate size of array. sizeof(tmArray); equals to the size of pointer, as tmArray is a pointer.

MiiNiPaa what you advice me?
1) create operator[]
It signature should be: tmArray& operator[](size_t pos);

2) Save array size alongside your buffer. You do not need to recalculate it every time.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
template <typename tmArray>
class arrays
{
    private:
        int arraysize{0};
        tmArray value[];
        bool blnResized{false};

    public:
        arrays(int arrayssize=0)
        {
            if(arrayssize!=0)
            {
                blnResized=true;
                value=(tmArray) malloc(arrayssize*sizeof(int));//error
            }
        }

"expected primary-expression before ')' token"
the parentheses are multiply of 2... strange.
the value isn't a pointer, because i want avoid the '->'.
what you can tell me?
Actually, incomplete declarations are directly forbidden by standard, not converted to pointer, my mistake.

So your code is not valid standard C++. It compiles for you because of some non-standard compiler extensions and who knows how they work...
can you be, please, more especific? please
tmArray value[]; is illegal C++.
error: definition of variable with array type needs an explicit size or an initializer
http://coliru.stacked-crooked.com/a/878f74828185fddc

Allocation of variable size arrays is done through pointer to first element:
1
2
3
4
5
char* ptr;
ptr = (char)malloc(20*sizeof(decltype(*ptr)));
free(ptr);
ptr = new char[20];
delete[] ptr;
i fix more than that error.
please see these 1 function:
1
2
3
4
tmArray& operator[](size_t pos)
        {
            return *this;
        }

from these line:
1
2
3
arrays<button> btnarray(8);

btnarray[i].setTop(inttop);

"In instantiation of 'tmArray& arrays<tmArray>::operator[](size_t) [with tmArray = button; size_t = unsigned int]'
"invalid initialization of reference of type 'button&' from expression of type 'arrays<button>'"
return *this; Of course it is a mismatch *this is arrays<tmArray>.

You want to return an element of undelying array.
http://en.cppreference.com/w/cpp/language/operators#Array_subscript_operator
error fixed. but see these:
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
template <typename tmArray>
class arrays
{
    private:
        int arraysize{0};
        tmArray *value;

    public:
        arrays(int intarrayssize=0)
        {
            arraysize=intarrayssize;
            if(arraysize!=0)
            {
                value= new tmArray[arraysize];
            }
        }

        void Resize(int intResize)
        {
            value =new tmArray[intResize];
        }

        int Size()
        {
            return arraysize;
        }

        ~arrays()
        {
            delete [] value;
        }

        tmArray& operator[](size_t pos)
        {
            return value[pos];
        }
};


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
arrays<button> btnarray(8);

int WinMain()
{
    //OutputDebugString("hello world");
    //int arraysize=ArraySize(btnarray, button);//geting the array size
    //btnarray.Resize(8);
    int arraysize=btnarray.Size();
    int i=0;
    for (i; i<arraysize; i++)
    {
        int inttop=150 + (i*50);

        btnarray[i].setTop(inttop);
        btnarray[i].setLeft(0);
        btnarray[i].setText(to_string(i));
        btnarray[i].MouseClick=[i]()
        {
            MessageBox(btnarray[i].getText());
        };
        DebugText((string)to_string(inttop) + "\t" + to_string(i));
    }

visible i only see the 0(zero) and from 3 to 7. why seen these 'error'?
but printed:

150 0
200 1
250 2
300 3
350 4
400 5
450 6
500 7

(i did an excelente function for do the Debug)
What exactly is the problem? Everything seems to be working.
i see more or less the same problem of the vectors :(
if i use a standar C arrays, the 8 button are showed normaly on exact calculation positions. but using the arrays\vectors i can lose some buttons :(
What is a button? Maybe there is a problem with this class.
the button 1(index) and button 2(index).
What is button? Where it is come from. It is not a standard class, so maybe class is just broken? What it default constructor does?
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
button( const button &buttoninstance)
    {
        setParent(GetParent(buttoninstance.hwnd));
        setHeight(buttoninstance.intHeight);
        setLeft(buttoninstance.intLeft);
        setTop(buttoninstance.intTop);
        setWidth(buttoninstance.intWidth);
        setTextAlignment(buttoninstance.intTextAlignment);
        intMouseHover=buttoninstance.intMouseHover;
        setAutoSize(buttoninstance.blnAutoSize);
        setEnable(buttoninstance.blnEnable);
        setTabStop(buttoninstance.blnTabStop);
        setTextWrap(buttoninstance.blnTextWrap);
        setTransparent(buttoninstance.blnTransparent);
        setVisible(buttoninstance.blnVisible);
        setBackColor(buttoninstance.clrBackColor);
        setTextColor(buttoninstance.clrTextColor);
        strCaption=buttoninstance.strCaption;
        hCursor=buttoninstance.hCursor;
        SetClassLong(hwnd,    // window handle
            GCL_HCURSOR,      // change cursor
            (LONG) hCursor);
        //HFONT a=(HFONT)SendMessage(buttoninstance.hwnd, WM_GETFONT, 0, 0);
//        setFont(a);
    }

    button & operator=( const button &buttoninstance)
    {
        setParent(GetParent(buttoninstance.hwnd));
        setHeight(buttoninstance.intHeight);
        setLeft(buttoninstance.intLeft);
        setTop(buttoninstance.intTop);
        setWidth(buttoninstance.intWidth);
        setTextAlignment(buttoninstance.intTextAlignment);
        intMouseHover=buttoninstance.intMouseHover;
        setAutoSize(buttoninstance.blnAutoSize);
        setEnable(buttoninstance.blnEnable);
        setTabStop(buttoninstance.blnTabStop);
        setTextWrap(buttoninstance.blnTextWrap);
        setTransparent(buttoninstance.blnTransparent);
        setVisible(buttoninstance.blnVisible);
        setBackColor(buttoninstance.clrBackColor);
        setTextColor(buttoninstance.clrTextColor);
        strCaption=buttoninstance.strCaption;
        hCursor=buttoninstance.hCursor;
        SetClassLong(hwnd,    // window handle
            GCL_HCURSOR,      // change cursor
            (LONG) hCursor);
        //HFONT a=(HFONT)SendMessage(buttoninstance.hwnd, WM_GETFONT, 0, 0);
        //setFont(a);
        return *this;
    }

    button(string caption="", HWND parent=WindowMain)
    {
        ++ButtonCount;
        if(caption=="")
            strCaption=strCaption + to_string(ButtonCount);
        else
            strCaption=caption;
        setParent(parent);
        altkey=GettingAltKey(strCaption);
        if(altkey!=-1)
            RegisterHotKey(hwnd,altmessage,MOD_ALT,altkey);
    }

void setParent(HWND parent=WindowMain)
    {
        if (hwnd==NULL)
        {
            WNDCLASS ButtonClass;
            HINSTANCE mod = (HINSTANCE)GetModuleHandle(NULL);

            ZeroMemory(&ButtonClass, sizeof(WNDCLASS));
            GetClassInfo(mod, TEXT("BUTTON"), &ButtonClass);

            ButtonClass.hInstance = mod;
            ButtonClass.lpszClassName = TEXT("CBUTTON");
            ButtonClass.style=CS_DBLCLKS;


            // store the old WNDPROC of the button window class
            SetProp(parent, buttonpropname, (HANDLE)ButtonClass.lpfnWndProc);
            // replace it with local WNDPROC

            ButtonClass.lpfnWndProc = WndProcButton;
            ButtonClass.hbrBackground = (HBRUSH)GetStockBrush(NULL_BRUSH);

            // register the new window class"
            RegisterClass(&ButtonClass);

            hwnd = CreateWindowEx(
                0,
                TEXT("CBUTTON"),//these must be the same of  ButtonClass.lpszClassName.. registed class
                strCaption.c_str(),
                WS_VISIBLE | WS_CHILD |WS_TABSTOP| BS_OWNERDRAW | BS_NOTIFY | WS_CLIPSIBLINGS, //onerdraw for add images
                intLeft, intTop, intWidth, intHeight,
                parent,
                NULL,
                mod,
                this);

            if (hwnd == NULL)
                MessageBox(NULL, "Can't create the control", "error", MB_OK);

            if (SetProp(hwnd, buttonclassprop, (HANDLE)this) == 0)
                MessageBox(NULL, "can't set the class property", "error", MB_OK);
            clrBackColor= GetBkColor(GetDC(GetParent(hwnd)));
            clrTextColor = GetTextColor(GetDC(hwnd));
        }
        else
        {
            SetParent(hwnd,parent);
        }

        /*RECT a;
        GetClientRect(hwnd,&a);
        intTop=a.top;
        intLeft=a.left;
        intWidth=a.right-a.left;
        intHeight=a.bottom-a.top;
        InvalidateRect(hwnd,NULL,true);//i add these line for fix that
        UpdateWindow(hwnd);
        SetWindowPos(hwnd, 0,intLeft ,  intTop, intWidth, intHeight,
                SWP_NOZORDER|SWP_NOACTIVATE|
                SWP_DRAWFRAME | SWP_FRAMECHANGED|SWP_NOCOPYBITS | SWP_NOMOVE | SWP_NOSIZE);*/


        mousestoped.timerprocedure=[this]()
        {
            static POINT Location, PreviousLocation={0};
            if(dcaButton==DrawControlsAction::MouseEnter)
            {
               GetCursorPos(&Location);
                //testing is previous position is igual to location
                //if they are the same, so the mouse is stop
                if ((Location.x == PreviousLocation.x) && (Location.y == PreviousLocation.y))
                {
                    MouseStoped();
                }
                else
                {
                    MouseButtons MBButtons;
                    bool blControl=false;
                    bool blShift=false;


                    if(GetKeyState(MK_CONTROL) & 0x8000)
                    {
                        blControl=true;
                    }

                    if(GetKeyState(MK_SHIFT) & 0x8000)
                    {
                        blShift=true;
                    }

                    if(GetKeyState(MK_LBUTTON) & 0x8000)
                    {
                        MBButtons=Left;
                        dcaButton=DrawControlsAction::MouseClick;
                        InvalidateRect(hwnd,nullptr,FALSE);
                    }
                    else if(GetKeyState(MK_RBUTTON) & 0x8000)
                        MBButtons=Right;
                    else if(GetKeyState(MK_MBUTTON) & 0x8000)
                        MBButtons=Middle;
                    else  if(GetKeyState(MK_XBUTTON1) & 0x8000)
                        MBButtons=X1;
                    else  if(GetKeyState(MK_XBUTTON2) & 0x8000)
                        MBButtons=X2;
                    MouseMove(MBButtons,blControl,blShift,Location.x,Location.y);
                }
            }
            InvalidateRect(hwnd,NULL,TRUE);
            PreviousLocation = Location;
        };
        mousestoped.Interval=100;
        mousestoped.Start();
        setAutoSize(true);
    }
Last edited on
I do not see many fields being initializated in default constructor. It might cause some problems. Try to assign working buttons to not working ones and then change them as needed. If it will work, then it is just problem of improper initialization of buttons.
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
//Button Class
const char buttonpropname[] = "propertybutton";
const char buttonclassprop[] = "classbutton";
const int buttonHotkey_Message=0;

class button
{
private:
    static int ButtonCount;
    image imgtest;
    HCURSOR hCursor=(HCURSOR)LoadCursor(GetModuleHandle(NULL),IDC_APPSTARTING);
    string strCaption="Button ";
    string strCursor="";
    color clrTextColor=0;
    color clrBackColor=0;
    bool blnTransparent=false;
    bool blnAutoSize=false;
    bool blnTabStop=true;
    bool blnTextWrap=false;
    bool blnMouseEnter=false;
    int intTop=0;
    int intLeft=0;
    int intWidth=70;
    int intHeight=30;
    int intBorder=0;
    int intMouseHover=HOVER_DEFAULT;
    HWND hwnd;
    bool blnVisible=true;
    bool blnEnable=true;
    int intTextAlignment=0;
    char altkey=-1;
    UINT altmessage=0;
    Timer animation;
    Timer mousestoped;
    DrawControlsAction dcaButton=DrawControlsAction::Normal;
    CHOOSEFONT chFont;
///..................... 
"I do not see many fields being initializated in default constructor. It might cause some problems. Try to assign working buttons to not working ones and then change them as needed. If it will work, then it is just problem of improper initialization of buttons."
what you mean by that?
not using:
1
2
3
classconstructor():variable1(x), variable2(y)
{
}

?
don't stay strange or mad with me. i'm trying understand in hard way. for fix all of my errors\bugs on code.
(like i did with debugger: the Code Blocks don't have a nice debugger so i did my own nice function for it. i can share it, but uses several of my class's: form and label(maybe with time i add the list viwe or text box))
Pages: 12