Looking for some help (Error: Expected a Expression)

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

#include "I:\Documents\Visual Studio 2010\Projects\Coding Club Game\Coding Club Game\blocks.h"
#include <allegro.h>
#include <string>


using namespace std;

blocks::blocks()
{
	place == {{true, true, true, true}, {true, true, true, true}};
}

void blocks::draw(BITMAP* buffer, ball &bl, int &score, SAMPLE* sound)
{
    functs f;

    for(int j = 0; j < 4; j++)
    {
    for(int i = 0; i < 4; i++)
    {
        if(f.collision(bl.x, bl.y, 10, 10, i*100+5, j*40 + 40, 100, 20) == true && place[j][i] == true)
        {
            place[j][i] = false;

            score++;

            play_sample(sound, 255, 128, 1000, false);

            if(bl.dir == "NE")
                bl.dir = "SE";
            else if(bl.dir == "NW")
                bl.dir = "SW";
            else if(bl.dir == "N")
                bl.dir = "S";
            else if(bl.dir == "S")
                bl.dir = "N";
            else if(bl.dir == "SE")
                bl.dir = "NE";
            else if(bl.dir == "SW")
                bl.dir = "NW";
        }

        if(place[j][i] == true)
            rectfill(buffer, i*100+5, j*40 + 40, i*100+100, j*40 + 60, makecol(0, 255, 0));
    }
    }
}



The error is Line 10:

 
place == {{true, true, true, true}, {true, true, true, true}};


The

 
place == 


works fine, it is the first

 
{ 


that is giving a error.
Last edited on
Bump, I could really use some help on this. I literally have no clue why it is expecting a expression when it is right there.
closed account (3qX21hU5)
== is for comparision I believe you are look for = which is assignment.

So for example.
place = {{true, true, true, true}, {true, true, true, true}};
When I delete the one equal sign, I get the same expected expression error and ontop of that I get a Expression must be a modifiable lvalue on place.
Perhaps place = {{true, true, true, true}, {true, true, true, true}};

@vin read my previous post.
closed account (3qX21hU5)
When I delete the one equal sign, I get the same expected expression error and ontop of that I get a Expression must be a modifiable lvalue on place.


place = {{true, true, true, true}, {true, true, true, true}};
Is what you are looking for since there would be no reason to have place == {{true, true, true, true}, {true, true, true, true}}; in the class's constructor not to mention it wouldn't make sense doing so anywhere else.

Could you post the header file also please? Also please copy and paste the exact error you are getting (While using the assignment operator).
Last edited on
http://www.filedropper.com/files_2

Theres the project folder I have for my project.
closed account (3qX21hU5)
Sorry not that I don't trust you but I don't download unknown files from people I don't know and trust (Specially when I am at work).

All I should need to see is the blocks.h header file. So if you could just copy and paste the contents of that file that would be great. Also the exact error message could help to.
blocks.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#ifndef BLOCKS_H
#define BLOCKS_H

#include <allegro.h>
#include "functs.h"
#include "ball.h"

class blocks
{
    public:
        blocks();
        void draw(BITMAP* buffer, ball &bl, int &score, SAMPLE* sound);

        bool place[4][4];
};

#endif // BLOCKS_H 


blocks.cpp
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
#include "blocks.h"
#include <allegro.h>
#include <string>

using namespace std;

blocks::blocks()
{
	place == {{true, true, true, true}, {true, true, true, true}};
};

void blocks::draw(BITMAP* buffer, ball &bl, int &score, SAMPLE* sound)
{
    functs f;

    for(int j = 0; j < 4; j++)
    {
    for(int i = 0; i < 4; i++)
    {
        if(f.collision(bl.x, bl.y, 10, 10, i*100+5, j*40 + 40, 100, 20) == true && place[j][i] == true)
        {
            place[j][i] = false;

            score++;

            play_sample(sound, 255, 128, 1000, false);

            if(bl.dir == "NE")
                bl.dir = "SE";
            else if(bl.dir == "NW")
                bl.dir = "SW";
            else if(bl.dir == "N")
                bl.dir = "S";
            else if(bl.dir == "S")
                bl.dir = "N";
            else if(bl.dir == "SE")
                bl.dir = "NE";
            else if(bl.dir == "SW")
                bl.dir = "NW";
        }

        if(place[j][i] == true)
            rectfill(buffer, i*100+5, j*40 + 40, i*100+100, j*40 + 60, makecol(0, 255, 0));
    }
    }
}
ball.h
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
#include "ball.h"
#include "global.h"
#include <allegro.h>

ball::ball()
{
    bMove = false;
    dir = "N";
    once = true;
}

void ball::init(int pX, int Size)
{
    if(bMove == false)
    {
    x = (pX+50)-(Size/2);
    y = 370 - Size;
    size = Size;
    }
}

void ball::input()
{
    if(key[KEY_SPACE])
        bMove = true;
}

void ball::move(int speed, int pX, int pY, bool &quit)
{
    if(y < 0 && once == true)
    {
        dir = "S";
        once = false;
    }
    if(y > 370 && x < pX+100 && x > pX)
    {
        if(x == pX+45)
        dir = "N";
        else if(x > pX+45)
        dir = "NE";
        else if(x < pX+45)
        dir = "NW";
    }

    if(x < 0)
    {
        if(dir == "NW")
            dir = "NE";
        else if(dir == "SW")
            dir = "SE";
    }

    if(x > sWidth)
    {
        if(dir == "NE")
            dir = "NW";
        else if(dir == "SE")
            dir = "SW";
    }

    if(y < 0)
    {
        if(dir == "NW")
            dir = "SW";
        else if(dir == "NE")
            dir = "SE";
        else if(dir == "N")
            dir = "S";
    }

    if(y > sHeight)
    {
        quit = true;
    }

    if(bMove == true && dir == "N")
    {
        y -= speed;
    }
    else if(dir == "S")
    {
        y += speed;
    }
    else if(dir == "SE")
    {
        x += (speed/2);
        y += speed;
    }
    else if(dir == "SW")
    {
        x -= (speed/2);
        y += speed;
    }
    else if(dir == "NE")
    {
        x += (speed/2);
        y -= speed;
    }
    else if(dir == "NW")
    {
        x -= (speed/2);
        y -= speed;
    }
}

void ball::show(BITMAP* buffer)
{
    circlefill(buffer, x, y, size, makecol(0, 0, 255));
}


functs.h
1
2
3
4
5
6
7
8
9
10
11
12
#ifndef FUNCTS_H
#define FUNCTS_H

class functs
{
    public:
        void input(int &x, int speed, int width);
        bool collision(int x, int y, int w, int h, int x2, int y2, int w2, int h2);
};

#endif // FUNCTS_H


global.h
1
2
3
4
5
6
7
8
#define sWidth 400
#define sHeight 400

#define bWidth 400
#define bHeight 400

void input(int &x);


ball.cpp

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
#include "ball.h"
#include "global.h"
#include <allegro.h>

ball::ball()
{
    bMove = false;
    dir = "N";
    once = true;
}

void ball::init(int pX, int Size)
{
    if(bMove == false)
    {
    x = (pX+50)-(Size/2);
    y = 370 - Size;
    size = Size;
    }
}

void ball::input()
{
    if(key[KEY_SPACE])
        bMove = true;
}

void ball::move(int speed, int pX, int pY, bool &quit)
{
    if(y < 0 && once == true)
    {
        dir = "S";
        once = false;
    }
    if(y > 370 && x < pX+100 && x > pX)
    {
        if(x == pX+45)
        dir = "N";
        else if(x > pX+45)
        dir = "NE";
        else if(x < pX+45)
        dir = "NW";
    }

    if(x < 0)
    {
        if(dir == "NW")
            dir = "NE";
        else if(dir == "SW")
            dir = "SE";
    }

    if(x > sWidth)
    {
        if(dir == "NE")
            dir = "NW";
        else if(dir == "SE")
            dir = "SW";
    }

    if(y < 0)
    {
        if(dir == "NW")
            dir = "SW";
        else if(dir == "NE")
            dir = "SE";
        else if(dir == "N")
            dir = "S";
    }

    if(y > sHeight)
    {
        quit = true;
    }

    if(bMove == true && dir == "N")
    {
        y -= speed;
    }
    else if(dir == "S")
    {
        y += speed;
    }
    else if(dir == "SE")
    {
        x += (speed/2);
        y += speed;
    }
    else if(dir == "SW")
    {
        x -= (speed/2);
        y += speed;
    }
    else if(dir == "NE")
    {
        x += (speed/2);
        y -= speed;
    }
    else if(dir == "NW")
    {
        x -= (speed/2);
        y -= speed;
    }
}

void ball::show(BITMAP* buffer)
{
    circlefill(buffer, x, y, size, makecol(0, 0, 255));
}


funts.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include "functs.h"
#include <allegro.h>
#include "global.h"

void functs::input(int &x, int speed, int width)
{
    if(key[KEY_RIGHT] && x+width < sWidth)
        x += speed;
    if(key[KEY_LEFT] && x > 0)
        x -= speed;
}

bool functs::collision(int x, int y, int w, int h, int x2, int y2, int w2, int h2)
{
	if(x+w < x2 || y+h < y2 || x > x2+w2 || y > y2+h2)
		return false;
	else
		return true;
}

main.cpp
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
182
183
184
185
#include <allegro.h>
#include "global.h"
#include "functs.h"
#include "ball.h"
#include "blocks.h"

int x = 200;

volatile long counter = 0;

void fpsinc(){counter++;}

int main()
{
	allegro_init();
	install_keyboard();
	install_sound(DIGI_AUTODETECT, MIDI_AUTODETECT, NULL);
	install_mouse();
	set_color_depth(16);
	set_gfx_mode(GFX_AUTODETECT_WINDOWED, sWidth, sHeight, 0, 0);

	LOCK_VARIABLE(counter);
	LOCK_FUNCTION(fpsinc);
	install_int_ex(fpsinc, BPS_TO_TIMER(90));

	set_window_title("BlockZ                                                                        v0.4");

	MIDI* bgsong = load_midi("song.mid");
	SAMPLE* destroy = load_sample("destroy.wav");

	BITMAP* buffer = create_bitmap(bWidth, bHeight);

	BITMAP* bg = load_bitmap("bg.bmp", NULL);
	BITMAP* player = load_bitmap("hi.bmp", NULL);
	BITMAP* menu = load_bitmap("menu.bmp", NULL);
	BITMAP* option_menu = load_bitmap("options.bmp", NULL);

	functs f;
	ball b;

	blocks blck;

	int score = 0;

	bool quit = false;

	bool options = false;

	bool bMenu = true;

	bool play = false;

    play_midi(bgsong, true);

    int midiVol = 255;
    int sampleVol = 255;

    int ex = 100;
    int why = 100;

	while(quit == false)
	{
	    set_volume(sampleVol, midiVol);

	    ex = mouse_x;
	    why = mouse_y;

        if(bMenu == true)
        {
            blit(menu, buffer, 0, 0, 0, 0, 400, 400);

            if(key[KEY_O])
            {
                options = true;
                bMenu = false;
            }

            if(f.collision(ex, why, 10, 10, 40, 100, 120, 50) == true)
            {
                rect(buffer, 40, 100, 160, 150, makecol(0, 255, 0));
                if(mouse_b & 1)
                {
                    play = true;
                    bMenu = false;
                }
            }
            if(f.collision(ex, why, 10, 10, 40, 190, 195, 36) == true)
            {
                rect(buffer, 35, 185, 240, 230, makecol(0, 255, 0));
                if(mouse_b & 1)
                {
                    options = true;
                    bMenu = false;
                }
            }
            if(f.collision(ex, why, 10, 10, 30, 270, 180, 46) == true)
            {
                rect(buffer, 30, 270, 210, 326, makecol(0, 255, 0));
                if(mouse_b & 1)
                {
                    quit = true;
                }
            }

            rectfill(buffer, ex, why, ex+10, why+10, makecol(0, 255, 0));
        }

        if(options == true)
        {
            blit(option_menu, buffer, 0, 0, 0, 0, 400, 400);
            if(key[KEY_B])
            {
                bMenu = true;
                options = false;
            }
            if(key[KEY_Q])
                midiVol = 85;
            if(key[KEY_W])
                midiVol = 170;
            if(key[KEY_E])
                midiVol = 255;
            if(key[KEY_R])
                midiVol = 0;
            if(key[KEY_1])
                sampleVol = 85;
            if(key[KEY_2])
                sampleVol = 170;
            if(key[KEY_3])
                sampleVol = 255;
            if(key[KEY_4])
                sampleVol = 0;

            if(f.collision(ex, why, 10, 10, 125, 330, 120, 35) == true)
            {
                rect(buffer, 125, 330, 245, 365, makecol(0, 255, 0));
                if(mouse_b & 1)
                {
                    bMenu = true;
                    options = false;
                }
            }

            rectfill(buffer, ex, why, ex+10, why+10, makecol(0, 255, 0));
        }

        if(play == true)
        {
            if(key[KEY_ESC])
            {
                bMenu = true;
                play = false;
            }
	    b.init(x, 10);

	    while(counter > 0)
	    {
	    b.move(4, x, 370, quit);

	    b.input();

        f.input(x, 4, 100);
        counter -= 1;
	    }

		blit(bg, buffer, 0, 0, 0, 0, 400, 400);
		blit(player, buffer, 0, 0, x, 370, 100, 20);
		blck.draw(buffer, b, score, destroy);
		textprintf_ex(buffer, font, 0, 0, makecol(255, 0, 0), -1, "Score: %i", score);
		b.show(buffer);
        }
		blit(buffer, screen, 0, 0, 0, 0, bWidth, bHeight);
	}

    destroy_sample(destroy);
    destroy_midi(bgsong);
	destroy_bitmap(buffer);
	destroy_bitmap(bg);
	destroy_bitmap(player);
	destroy_bitmap(menu);
	destroy_bitmap(option_menu);

	return 0;
}
END_OF_MAIN();
closed account (3qX21hU5)
Ok first I want to say that you might want to go back and relearn your operators a bit better. In this case you are trying to use the '==' equality comparison operator to do assignment which should be done with the assignment operator '='.

But to fix the problem we need to change how we initializing the variable "place". If we changed the '==' operator to '=' operator it still wouldn't work because we are trying to assign a initializer list to a array that has already been created and initialized.

The only time you can use a initializer list to create a array or container is when you are first initialize it (Hence the name) IE bool myArray = {1, 2, 3};. So in your case it is illegal because the variable "place" has already been created by the time it hits the constructors code and it is doing a assignment to the variable "place" instead of a initialization.

Lucky there is a easy to way to fix this with a constuctor initialization list here is article that might help understand it http://www.learncpp.com/cpp-tutorial/101-constructor-initialization-lists/ (It might not be the best it was just the first google result for me).

We would implement it like this

1
2
3
blocks::blocks() : place({{true, true, true, true}, {true, true, true, true}})
{
};


The reason why this works and the other way doesn't is because when you use constructor initialization lists you are not assigning to the variable instead you are initializing them. These two things may seem similar but they are quite different and it is important to understand the differences between them.

1
2
3
4
5
int main()
{
    int number = 5;   // Initialization
    number = 10;      // Assignment
}




So in short to fix this just change your constructors implementation to this.

1
2
3
blocks::blocks() : place({{true, true, true, true}, {true, true, true, true}})
{
};


I would also recommend reading up on operators, assignment versus initialization and initialization lists/constructor initialization lists.

Anyways hope that help explain some things and sorry if it is quite hard to follow.
Last edited on
I have actually fixed my above error, but I am now getting

1
2
3
4
5
6

1>main.obj : error LNK2019: unresolved external symbol "public: __thiscall blocks::blocks(void)" (??0blocks@@QAE@XZ) referenced in function "int __cdecl _mangled_main(void)" (?_mangled_main@@YAHXZ)
1>MSVCRTD.lib(crtexe.obj) : error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup
1>I:\Documents\Visual Studio 2010\Projects\Coding Club Game\Debug\Coding Club Game.exe : fatal error LNK1120: 2 unresolved externals
1>


Do not know if I couldn classify my previous fix as a fix. But I have narrowed down my current error to not having something added into my linker from external dependencies
closed account (3qX21hU5)
Please see my above post. Also when you get another error message when you change something it doesn't mean you fixed the old problem ;p
Zereo, I had actually had it set to
1
2
3
4
blocks::blocks()
{
	place = {{true, true, true, true}, {true, true, true, true}};
}

but my instructor had told me to place two equal signs. As my project for VS is at school I will try your solution in the morning. Will post results then. Thanks.
Topic archived. No new replies allowed.