Array error - "no match for 'operator[]' in 'map[i]'

Apr 2, 2011 at 7:09pm
Im not sure if this is a beginner question, but I definitely am a beginner so this board seems best.

Im currently trying to work out a problem in a platformer style game I am working on, and have scoured the net extensively but can't find a solution.

Basically, I have a "Tile" class for terrain tiles, and an array of tiles called "map". In the input-handling code for the player sprite, I want to cycle through all the tiles contained in the map array. However, the project won't compile and instead gives me an error, saying "no match for 'operator[]' in 'map[i]'". I'm using Dev-C++ if that helps in any way.

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
//globals.h

#ifndef GLOBALS_H
#define GLOBALS_H

#include "window.h"
#include "creature.h"
#include "player.h"
#include "tile.h"
#include "SDL/SDL.h"
#include <string>
using namespace std;

const int FPS = 20; 

extern int screenW;
extern int screenH;

//this is the map array
extern Tile map; 
extern Creature mobList;
extern Window mainWindow;
extern Creature yeti;
extern SDL_Event event; 

#endif 


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
//main.cpp

#include "SDL/SDL.h"
#include "SDL/SDL_image.h" 
#include "globals.h"
#include "image.h"
#include "timer.h"
#include "tile.h"
#include <string>
using namespace std;

int screenW = 800;
int screenH = 600;

int main (int argc, char* args[]) {
    SDL_Init(SDL_INIT_EVERYTHING);
    bool quit = false;
    Timer timer (FPS);
    Window mainWindow (screenW, screenH, false);
    //Tile terrain ("terrain.png", 301, 118, 300, 200, 301, 118, 300, 200);
    Tile map [2] = {Tile ("terrain.png", 301, 118, 300, 200, 301, 118, 300, 200),
                    Tile ("terrain.png", 301, 118, 300, 400, 301, 118, 300, 200)};
    Creature mobList [1] = {Creature ("yeti.png", 149, 194, 0, 0, 10)};
    Player player ("player.png", 93, 150, 200, 200, 93, 150, 200, 200);
    SDL_Event event;
   
    while (!quit) {
        timer.start();
        
        while (SDL_PollEvent(&event)) {
            if (event.type == SDL_QUIT) {
                quit = true;
            }
        }
        player.input();
        
        mainWindow.fill(130, 150, 150);
        player.blit(mainWindow.get_screen()); 
        for (int i=0; i<1; i++) {
            mobList[i].blit(mainWindow.get_screen());
        } 
        for (int i=0; i<2; i++) {
            
            //***This here works, however***
            map[i].blit(mainWindow.get_screen());
        } 
        mainWindow.update();
        
        timer.pause();
    };

    return 0;
}


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
//player.cpp

#include "SDL/SDL.h"
#include "player.h"
#include "globals.h"
#include "image.h"
#include "tile.h"

/*
Other player functions
*/

void Player::input () {
    Uint8 *keystates = SDL_GetKeyState(NULL); 
    if (keystates[SDLK_LEFT]) {
        sprite.set_x(sprite.get_x()-speed);
    }
    if (keystates[SDLK_RIGHT]) {
        sprite.set_x(sprite.get_x()+speed);
    }
    for (int i=0; i<2; i++) {
        
        //***This line here is where it gets stuck***
        if (get_touching(map[i].get_rect(), rect)) {
            //beautifully written code will go here
        }
    }
}


Apologies for the messiness of the code. I think I've posted everything relevant here, sorry if not. I would really appreciate feedback on this, I've been losing losing sleep over this problem for far too long.

Thanks,
Alex
Apr 2, 2011 at 7:16pm
map[] appears undeclared in your function, and you aren't passing anything to the function. so do you think it could be a problem with map[] being undeclared? or is it global?
Apr 2, 2011 at 8:35pm
I believe map is declared in "globals.h", which is included in the player file.
Apr 2, 2011 at 8:43pm
You cannot have all your globals be extern, they must be declared somewhere.
Apr 2, 2011 at 8:47pm
In "main.cpp" map is declared with:
1
2
Tile map [2] = {Tile ("terrain.png", 301, 118, 300, 200, 301, 118, 300, 200),
                         Tile ("terrain.png", 301, 118, 300, 400, 301, 118, 300, 200)};

That should work, no?
Apr 2, 2011 at 9:02pm
yes, but in order to use it you need to pass it to the function or else it is undeclared

example:

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

#include <iostream>


using namespace std;
void F1 ();
void F2 (int);

int main()
{
 int a=2;
 F1(); 
 F2(a);



}
void F1()
{
  cout<<a<<endl;
//this will cause an error because a is undefined

}

void F2(int a)
{
  cout<<a<<endl;
//this will work because you passed the value of a to the function

}


Last edited on Apr 2, 2011 at 9:05pm
Apr 3, 2011 at 11:23am
Thanks very much! The map is now passed to the input function and everything works perfectly.
Topic archived. No new replies allowed.