Problem with vector of Vector of sprites

In my project i need make a snake represented by a chain of Sprites. I want store that sprites in two-dimensional vector. But despite all my attempts I stil get error.

header file:
std::vector<std::vector<Sprite*> > SnakesSprites;

ccp file:
Vector <Sprite*> Snake;

error C2039: ‘__this’: is not a member of ‘std::vector<std::vector<cocos2d::Sprite *,std::allocator<_Ty>>,std::allocator<std::vector<_Ty,std::allocator<_Ty>>>>’
_Ty=cocos2d::Sprite *

Sorry for the mistakes, English is not my first language.

You need to show more code.

1 Like
	auto SnakeHead = Sprite::create("snake_head.png");		
	if (SnakeHead == nullptr) { problemLoading("'SnakeHead.png'"); }

	int Sx = CageSize * (rand() % (CageWidth - 1) + 1);
	int Sy = CageSize * (rand() % (CageHeigth - 1) + 1);

	SnakeHead->setPosition(Sx, Sy);
	Vector <Sprite*> Snake;

What is the object type of addChild?

Your vector scope will be quite limited I.e only available to the function it is created in.

I think you should post more broadly about that you are doing and what your goal is.

The code is not correct.

You’ve created this:

std::vector<std::vector<Sprite*> > SnakesSprites;

Yet you’re trying to add a cocos2d::Vector<Sprite*> to the SnakesSprites std::vector:

Vector <Sprite*> Snake;

std::vector and cocos2d::Vector are not the same.

Also, std::vector does not have a method named pushBack; it should be push_back.


I’m trying create somthing like backwards classic snake game. Player plants flowers, snakes trying eat that. To control snake movement and interactions i wanna create vector. It’s my first cocos project, so i copy lot of things, not fully understanding how them work.

addChild - i use that to add sprites to scene. It working)))

Probably better upload all my code
HelloWorldScene.cpp (4.6 KB)
HelloWorldScene.h (1.7 KB)

I totally thought this was a typo :slight_smile:

It’s best to understand what it is you’re copying, otherwise you’re going to keep running into problems like this.

Thank you very much. It helps.
I trying std:: vector <Vector<Sprite*> > to, but i use pushBack to add vectors in it

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.