I’ve looked through other questions on this error but they’re not helping me. I’m still new to C++ and I don’t know if I’m misunderstanding something, misusing something, or just plain missing something but I can’t figure out what and I need some help. My error I’m getting is
SpriteImage does not name a type
Sprite.cpp
#include "Sprite.h"
#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include <iostream>
#include <vector>
namespace ColdFusion
{
Sprite::Sprite()
{
bufferSize = 100;
}
void Sprite::addToBuffer(SpriteImage img)
{
if(SpriteBuffer->size() == bufferSize)
{
bufferSize *= 2;
SpriteBuffer->resize(bufferSize);
}
SpriteBuffer->push_back(img);
}
SpriteImage Sprite::getFromBuffer(int index) //**Here is the error**
{
return SpriteBuffer[index];
}
void Sprite::removeFromBuffer(int index)
{
SpriteBuffer->erase(SpriteBuffer.begine()+index);
}
void Sprite::removeFromBuffer(SpriteImage index)
{
for(int j = 0; j <= SpriteBuffer->size(); j++)
{
if(SpriteBuffer[j].name == index.name)
{
removeFromBuffer(j);
}
}
}
void Sprite::applyToScreen(SpriteImage img, SDL_Surface* destination)
{
applyImage(img.x, img.y,getImage(img.filepath),destination)
}
SDL_Surface* Sprite::getImage(std::string filepath)
{
SDL_Surface* optimized = NULL;
SDL_Surface* loaded = NULL;
loaded = IMG_Load(filepath.c_str());
if(loaded != NULL)
{
optimized = SDL_DisplayFormat(loaded);
SDL_FreeSurface(loaded);
return optimized;
}
return NULL;
}
void Sprite::applyImage(int x, int y, SDL_Surface* source, SDL_Surface* destination)
{
SDL_Rect offset;
offset.x = x;
offset.y = y;
SDL_BlitSurface(source, NULL, destination, &offset);
}
Sprite::~Sprite()
{
}
}
Sprite.h
#ifndef SPRITE_H
#define SPRITE_H
#include "SDL/SDL.h"
#include <iostream>
#include <vector>
namespace ColdFusion
{
class Sprite
{
public:
Sprite();
~Sprite();
typedef struct SpriteImage{
int x;
int y;
std::string name;
std::string filepath;
};
void addToBuffer(SpriteImage img);
SpriteImage getFromBuffer(int index);
void removeFromBuffer(int index);
void removeFromBuffer(SpriteImage index);
void applyToScreen(SpriteImage img, SDL_Surface* destination);
protected:
private:
int bufferSize;
std::vector<SpriteImage> SpriteBuffer[100];
SDL_Surface* image;
SDL_Surface* getImage(std::string filepath);
void applyImage(int x, int y, SDL_Surface* source, SDL_Surface* destination);
};
}
#endif
SpriteImageis a nested type and you have to qualify it with the name of its surrounding class to access it, when you are outside of the scope of the containing class. UseSprite::SpriteImage. It also would make sense to take the parameter toaddToBufferbyconst&to avoid to many copy constructions. (Probably they end up being optimized away, but it is good practice).Instead of a raw array, you can also use
std::arrayorboost::array. On second though: The declarationstd::vector<SpriteImage> SpriteBuffer[100];probably doesn’t declare what you think it does.