Public Group

# class /A/ = std::vector/B/.end()

This topic is 5398 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

ok i have the above senario my classes are is block i = Worm_blocks.end();
#include "SDL.h"
#include <windows.h>
#include <string>
using namespace std;
#ifndef _BLOCK_H_
#define _BLOCK_H_
typedef DWORD     STATE;
const STATE       MT =0,
pointU =1,
pointD =2,
pointL =3,
pointR =4,
filled =5;
typedef DWORD     BLOCK_POS;
follower=0;

class block
{
public:
int Grid[10][10];
block(){};
~block(){};
void Move();
void Eating();
SDL_Surface *Image;
int x,y;
int velocityX,velocityY;
int block_tileX, block_tileY;
STATE block_state;
BLOCK_POS block_pos;
};
#endif


Worm
#include "SDL.h"
#include "Block.h"
#include <windows.h>
#include <vector.h>
#ifndef _WORM_H_
#define _WORM_H_
class Worm
{
public:
Worm(){};
~Worm(){};
std::vector<block> Worm_Blocks;
int X,Y;
int height;
int length;
void Eating();
};
#endif


i just want to find the block at the end and use it as a parameter to a function i havent worked with vectors a whole lot so i would apriciate your help or if there is another way to go about doing this so that i wont have this problem let me know too ERROR: Worm.cpp:5: error: cannot convert __gnu_cxx::__normal_iterator<block*, std::vector<block, std::allocator<block> > >' to block*' in initialization i know whats its saying i just dont know how to fix it thanks again [smile]

##### Share on other sites
vector.end returns an iterator that points to one-past-the-end of the vector. Your then taking an iterator, and assigning it to a different type of object, which causes the compilers type check to fail.

You could do *(vector.end()-1), or just call vector.back(), which returns a reference to the last object.

Obviously, the latter is preferable, but try both. Especially try try the first while cutting out the -1 and the * and watch your results. I mentioned the former to give you some idea of how iterators work. You really should read up on them.

##### Share on other sites
oo it gose one PAST the end ok that makes more since not i cant assaign a block a to NULL ok i got it working
thanks alot!! [grin]

• 18
• 29
• 11
• 21
• 16