   ...was not declared in scope
  Hello,I have the following errors:g++ -O2 -Wall -Werror -ansi   -c -o main.o main.cppg++ -O2 -Wall -Werror -ansi   -c -o display.o display.cppg++ -O2 -Wall -Werror -ansi   -c -o event.o event.cppg++ -O2 -Wall -Werror -ansi   -c -o sprite.o sprite.cppsprite.cpp: In member function ‘void Sprite::show()’:sprite.cpp:15: error: ‘sun1’ was not declared in this scopesprite.cpp:15: error: ‘screen’ was not declared in this scopesprite.cpp:16: error: ‘sun2’ was not declared in this scopesprite.cpp:16: error: ‘screen’ was not declared in this scopesprite.cpp:17: error: ‘sun3’ was not declared in this scopesprite.cpp:17: error: ‘screen’ was not declared in this scopesprite.cpp:18: error: ‘sun4’ was not declared in this scopesprite.cpp:18: error: ‘screen’ was not declared in this scopemake: *** [sprite.o] Error 1sun1 through sun4 and screen are declared in display.cpp.  I am trying to call the following function in sprite.cpp:void Sprite::show(){if(frame == 0) {apply_surface(x, y, sun1, screen);}if(frame == 1) {apply_surface(x, y, sun2, screen);}if(frame == 2) {apply_surface(x, y, sun3, screen);}if(frame == 3) {apply_surface(x, y, sun4, screen);}frame++;if(frame >= 4) {frame = 0;}}I am not sure of the best way to handle this.  I could write a function( {sun1-4();} ) for Sprite::show() and avoid using sun1-4 and screen altogether.  I would just put the function( void sun1-4(); ) in my display.cpp file, but I'm am not sure if this is the correct way or if there is a simpler way to do this.  Any thoughts?   
  Can you tell us more about your Sprite class? Maybe posting the declaration.You probably want to use an array for the surfaces.   ;  Are sun1 and friends global variables?   ;sprite.h#ifndef SPRITE_H#define SPRITE_Hclass Sprite{private://coordinates of the spriteint x, y;//its current time frameint frame;public://initialize the variablesSprite();//shows the spritevoid show();};#endifsprite.cpp#include <SDL/SDL.h>#include "sprite.h"#include "display.h"Sprite::Sprite(){x = 0;y = 0;frame = 0;}void Sprite::show(){if(frame == 0) {apply_surface(x, y, sun1, screen);}if(frame == 1) {apply_surface(x, y, sun2, screen);}if(frame == 2) {apply_surface(x, y, sun3, screen);}if(frame == 3) {apply_surface(x, y, sun4, screen);}frame++;if(frame >= 4) {frame = 0;}}sun1 and friends are not global variables.  I have a file layout.h that has some global variables like screen dimensions.   ;  If sun1 et al are neither global, nor members of the sprite class, nor local varialbes in Sprite::show(), how could that function know what they are?   ;  sun1 and friends are in display.cpp.  Here is the relevant part of display.cpp.  Since I have #included display.h in sprite.cpp wouldn't they be seen by sprite.cpp?  This is what I am not sure about, how to get sprite.cpp to see that they exist in display.cpp.#include "display.h"#include "layout.h"#include <SDL/SDL.h>#include "SDL/SDL_image.h"#include <stdexcept>#include <string>using namespace std;SDL_Surface *screen;SDL_Surface *background;SDL_Surface *sun1;SDL_Surface *sun2;SDL_Surface *sun3;SDL_Surface *sun4;.........//load the filesvoid load_files(){background = load_image("data/bg.bmp");sun1 = load_image("data/sun/1.bmp");sun2 = load_image("data/sun/2.bmp");sun3 = load_image("data/sun/3.bmp");sun4 = load_image("data/sun/4.bmp");if(!background || !sun1 || !sun2 || !sun3 || !sun4){throw runtime_error("Could not load the file.");}};Quote:Original post by Hop chewersun1 and friends are not global variables.  I have a file layout.h that has some global variables like screen dimensions.Well, then they are undeclared variables so you should probably declare them somewhere.  Note: when you compile sprite.cpp it only looks at sprite.cpp(#include is a textual replacement with the file included.)  If you would like it to look at some other file you must include it.  In this case you probably want to put an extern definition in sprite.cpp for the variables, or stop using global variables.   ;  Here is one way of doing it:// sprite.hpp// You could move some function implementations to sprite.cpp//class Sprite{public:    template<class Iterator>    Sprite(int x, int y, Iterator framesBegin, Iterator framesEnd)    :   frames(framesBegin, framesEnd),   currentFrame(0),   x(x),   y(y)    {    }    void show(SDL_Surface *screen)    {   apply_surface(x, y, frames[currentFrame], screen);   currentFrame = (currentFrame + 1) % frames.size();    }private:    std::vector<SDL_Surface *> frames;    int currentFrame;    int x, y;};// main.cpp//int main(){    SDL_Surface *surfaces[4] =    {   IMG_Load("sun1.png"),   IMG_Load("sun2.png"),   IMG_Load("sun3.png"),   IMG_Load("sun4.png")    };    Sprite sun(sunx, suny, surfaces, surfaces + 4);    while(running)    {   // ...   sun.show(screen);    }    // clean up surfaces}So, the point of this is that the sprite doesn't know what frames it will show. Nor does it have some upper limit. It can show any number of frames. std::vector<> is a dynamic array, it can grow to fit any number of elements.Now the sprite class is easier to use, you can use it for the "sun" sprite and any other sprites you may want to use...   ;Quote:Original post by Hop chewersun1 and friends are in display.cpp.  Here is the relevant part of display.cpp#include "display.h"#include "layout.h"#include <SDL/SDL.h>#include "SDL/SDL_image.h"#include <stdexcept>#include <string>using namespace std;SDL_Surface *screen;SDL_Surface *background;SDL_Surface *sun1;SDL_Surface *sun2;SDL_Surface *sun3;SDL_Surface *sun4;Those are globals. You need to declare them as extern in your sprite cpp, or in a header included by that cpp.   ;  I put sun1 and friends in layout.h and #include layout.h in sprite.cpp.  I don't get any errors anymore.#ifndef LAYOUT_H#define LAYOUT_H//set dimensions of screen and the bits per pixelconst int SCREEN_WIDTH = 640;const int SCREEN_HEIGHT = 480;const int SCREEN_BPP = 32;extern SDL_Surface *screen;extern SDL_Surface *sun1;extern SDL_Surface *sun2;extern SDL_Surface *sun3;extern SDL_Surface *sun4;#endifIs this correct?  Should I remove these from display.cpp?   