Getting vector with a pointer

I really don't want to use a pointer here, but I think I have to. My problem is that I have two classes that both need members from each other, so I need to use forward declaration + pointers, etc. However, trying to access a vector from a pointer throws an error. Here's the code:

1
2
3
4
5
for(int j = 0; j < level->GetCollisionObjects().size(); j++){
				if(fr.intersects(level->GetCollisionObjects()[j])){
					rotate = false;
				}
			}


level is a pointer to class Level. This function is inside class GamePiece. Class Level includes class GamePiece, but GamePiece needs access to the vector CollisionObjects. This function throws a vector size error, and I'm totally lost on what to do.
Last edited on
Create a new vector, set it equal to level->GetCollisionObjects() (assuming, of course, that that function returns a vector). Use that new vector for accessing particular values.
Please post the declaration of GetCollisionObjects and the errors you're receiving.
Last edited on
I solved it using a method I am using to pass another object around to all of my classes, an image manager. Here is what I did:
1
2
3
4
GamePiece::GamePiece(ImageManager &im, std::vector<sf::FloatRect> &levelCollisions) : imgr(im), levelCollisions(levelCollisions){
	pieceShape.setPosition(10, 0);
	SetShape();
}


That's my GamePiece constructor, and it takes a reference of Level's vector that I'm trying to get: std::vector<sf::FloatRect> &levelCollisions;

Then when I create my GamePiece object in my Level class, I pass on Level's vector as a parameter:
1
2
3
4
void Level::AddPiece(){
	GamePiece piece(imgr, levelCollisions);
	gamePieces_.push_back(piece);
}


My question is, is this a good way to solve it? In larger programs would I be passing on 20+ parameters, or is there a more efficient way to do this?
Topic archived. No new replies allowed.