Sign in to follow this  

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

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

If you intended to correct an error in the post then please contact us.

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;
const BLOCK_POS   leader =1,
                  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 this post


Link to post
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 this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this