c++11 for(each) loop

closed account (Dy7SLyTq)
so i am using g++ with code::blocks, and i having trouble with my for loop. gcc will use c++11 (thats not the issue), but i cant find why my for loop doesnt work.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include <vector>

using std::vector;

int main(int argc, char *argv[])
{
    SDL_Surface *Screen = NULL;
    vector<SDL_Surface> Button(5);

    for(auto Counter : Button)
        Button[Counter] = NULL;
}


i also tried it as Counter = NULL.

the errors are:

C:\Users\DTSCode\Desktop\SDL\Mirrors of Elixia\main.cpp||In function 'int SDL_main(int, char**)':|
C:\Users\DTSCode\Desktop\SDL\Mirrors of Elixia\main.cpp|13|error: no match for 'operator=' in 'Counter = 0'|
C:\Users\DTSCode\Desktop\SDL\Mirrors of Elixia\main.cpp|13|note: candidates are:|
C:\Users\DTSCode\Desktop\SDL\SDL Dev\include\SDL\SDL_video.h|96|note: SDL_Surface& SDL_Surface::operator=(const SDL_Surface&)|
C:\Users\DTSCode\Desktop\SDL\SDL Dev\include\SDL\SDL_video.h|96|note:   no known conversion for argument 1 from 'int' to 'const SDL_Surface&'|
C:\Users\DTSCode\Desktop\SDL\SDL Dev\include\SDL\SDL_video.h|96|note: SDL_Surface& SDL_Surface::operator=(SDL_Surface&&)|
C:\Users\DTSCode\Desktop\SDL\SDL Dev\include\SDL\SDL_video.h|96|note:   no known conversion for argument 1 from 'int' to 'SDL_Surface&&'|
C:\Users\DTSCode\Desktop\SDL\Mirrors of Elixia\main.cpp|9|warning: unused variable 'Screen' [-Wunused-variable]|
C:\Users\DTSCode\Desktop\SDL\Mirrors of Elixia\main.cpp|14|warning: no return statement in function returning non-void [-Wreturn-type]|
||=== Build finished: 1 errors, 2 warnings (0 minutes, 0 seconds) ===|
You probably want to store pointers in the vector because that is what the SDL load functions return. It is also necessary if you want to be able to set them to null.
 
vector<SDL_Surface*> Button(5);


The type of Counter will be the type that you are storing inside Button. If you made the change I suggested above it would be SDL_Surface*. To be able to change the pointer you also want to make it a reference.
1
2
for(auto& button : Button)
	button = NULL;

closed account (Dy7SLyTq)
thanks ill try that
@Peter87

1
2
for(auto& button : Button)
button = NULL;


I'm not sure this 2nd option can work. References cannot be NULL...

closed account (Dy7SLyTq)
it worked actually
> I'm not sure this 2nd option can work. References cannot be NULL...
You misread it.
It simply means that button is the element and no a copy, so changes would affect them.
It's not the reference that is set to null, but the pointer. Maybe it's more clear if you write the type explicitly instead of using auto.
1
2
for(SDL_Surface*& button : Button)
	button = NULL;
Last edited on
Topic archived. No new replies allowed.