declared by function

MCLoad.h - MCLoad.cpp
1
2
3
4
5
6
bool MCLoad::Load(SDL_Surface* Name, std::string File){
    Name = NULL;
    Name = SDL_LoadBMP(File.c_str());
    if(Name == NULL){return false;}
    else{return true;}
}

and here in MCInit.h - MCInit.cpp:
if((MCLoad::Load(&bg, "gfx\\bg.bmp")) == false){return false;}
and cleaning inside MCClean.h - MCClean.cpp
1
2
3
4
5
void MCClean::Clean(){
    SDL_FreeSurface(MCDisplay);
    SDL_FreeSurface(bg);
    SDL_Quit();
}

compiler gives me the error bg was not declared in this scope. what should i do. where i m mistaken.
i googled it. but truely speaking i m lost.
and maybe u might need these

in MCInit #include <SDL/SDL.h> #include "MCLoad.h"
in MCLoad #include <SDL/SDL.h> #include <string>
in MCClean #include <SDL/SDL.h> #include "MC.h"
Last edited on
What is bg and where do you declare it?
bg goes through my load function and declared by load function itself.
bool MCLoad::Load(SDL_Surface* Name, std::string File)

EDIT: does my project so comlicated? maybe i should simplify it
Last edited on
will u help me pls
is what i did foolish or idiotic or something. i dont know maybe what i did wasnt what i think i did hah. is the problem me asking something so obvious that i m not aware. someone pls tell me something
Last edited on
You haven't shown us enough code to see what's wrong. In particular, you haven't shown us where bg is defined. Hell, you haven't even told us what line causes the compiler error.

Why would you claim to want help, and then withhold the information that would enable us to give you that help?
well i wasnt aware that i m not giving enough info. sorry for that. so that was the thing so obvious that i couldnt see(the part i was foolishly mistaken) :D

algorithm of MC:
MCLoad.h->MCInit.h-->MC.h->MCMain.cpp
MCClean.h-------------| //it goes truogh MC.h

well Load func which is inside MCLoad.cpp takes two variables Name and File and use them to declare an SDL_Surface and creates a surface(see at my first post). this is how bg declared. a function that creates and declares SDL_Surface itself. this is where bg declared. MCLoad::Load(&bg, "gfx\\bg.bmp")

NOTE: MC.h is a header that contains other headers and does nothing it is just a bridge between MCMain.cpp and headers

EDIT: if u want any further info just say it i will anything that is necessery. in my first post i didnt wanted to make u drown with all the project :D
Last edited on
sorry for this but i thought it might be helpful
MCLoad.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#ifndef MCLOAD_H
#define MCLOAD_H

#include <SDL/SDL.h>
#include <string>

class MCLoad
{
    public:
        MCLoad();
        virtual ~MCLoad();
        static bool Load(SDL_Surface* Name, std::string File);
    protected:
    private:
};

#endif // MCLOAD_H 


MCLoad.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include "../include/MCLoad.h"

MCLoad::MCLoad()
{
    //ctor
}

MCLoad::~MCLoad()
{
    //dtor
}

bool MCLoad::Load(SDL_Surface* Name, std::string File){
    Name = NULL;
    Name = SDL_LoadBMP(File.c_str());
    if(Name == NULL){return false;}
    else{return true;}
}


MCInit.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#ifndef MCINIT_H
#define MCINIT_H
#include "MCLoad.h"

class MCInit
{
    public:
        MCInit();
        virtual ~MCInit();
        static bool Init();
    protected:
    private:
};

#endif // MCINIT_H 


MCInit.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include "../include/MCInit.h"
#include "../include/MC.h"
#include <SDL/SDL.h>

MCInit::MCInit()
{
    //ctor
}

MCInit::~MCInit()
{
    //dtor
}

bool MCInit::Init(){
    if(SDL_Init(SDL_INIT_EVERYTHING) == -1){return false;}

    if((MCDisplay = SDL_SetVideoMode(MCWidth, MCHeight, MCBpp, SDL_HWSURFACE | SDL_DOUBLEBUF)) == NULL){return false;}

    if((MCLoad::Load(&bg, "gfx\\bg.bmp")) == false){return false;}
    return true;
}


MC.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#ifndef MC_H
#define MC_H

#include "MCInit.h"
#include "MCLoop.h"
#include "MCClean.h"

int MCWidth             = 640;
int MCHeight            = 480;
int MCBpp               = 32;
SDL_Surface* MCDisplay  = NULL;

class MC
{
    public:
        MC();
        virtual ~MC();
    protected:
    private:
};

#endif // MC_H 


MCMain.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#ifdef __cplusplus
    #include <cstdlib>
#else
    #include <stdlib.h>
#endif

#include "include/MC.h"
#include <SDL/SDL.h>

int main ( int argc, char** argv )
{
    if((MCInit::Init()) == false){return 1;}

    return 0;
}
And where is bg declared?!
Nowhere in the code you've posted, have you declared bg. How is the compiler supposed to know what type it is, if you don't declare it?
it feels like i dont know how to speak dudes.

doesnt load function declares bg here when i used
1
2
3
4
5
6
bool MCLoad::Load(SDL_Surface* Name, std::string File){
    Name = NULL;
    Name = SDL_LoadBMP(File.c_str());
    if(Name == NULL){return false;}
    else{return true;}
}


1
2
3
4
5
6
7
bool MCInit::Init(){
    if(SDL_Init(SDL_INIT_EVERYTHING) == -1){return false;}

    if((MCDisplay = SDL_SetVideoMode(MCWidth, MCHeight, MCBpp, SDL_HWSURFACE | SDL_DOUBLEBUF)) == NULL){return false;}

    if((MCLoad::Load(&bg, "gfx\\bg.bmp")) == false){return false;}
    return true;


this
if((MCLoad::Load(&bg, "gfx\\bg.bmp")) == false){return false;}

1
2
3
4
5
6
int main ( int argc, char** argv )
{
    if((MCInit::Init()) == false){return 1;}

    return 0;
}



simply telling main calls MCInit::Init func. Init calls Load with using bg and where bg.bmp is. load declares bg at runtime and loads it. then the problem accurs. i cant call bg which is created at runtime it doesnt exist before program run.

NOTE:do u think i m and idiot. if u want i can delete myself from life
Last edited on
it feels like i dont know how to speak dudes.

Well, it would be trivially easy for you to go and look up what the word "declare" means in the context of C++ programming, if you really felt that you were confused about what it means.

load declares bg at runtime and loads it.

In the definition of Load that you've posted:

1
2
3
4
5
6
bool MCLoad::Load(SDL_Surface* Name, std::string File){
    Name = NULL;
    Name = SDL_LoadBMP(File.c_str());
    if(Name == NULL){return false;}
    else{return true;}
}

"bg" isn't even mentioned. It's certainly not declared.

EDIT: And it's not declared anywhere else in the code you've posted. You simply suddenly start using the name "bg" without ever declaring it.




Last edited on
the prob here is i thought i would take bg as Name and would declare it like a popcorn. and where i m confused is how it worked when i used it in my main to try and not working in my class. seriously how did i make it worked(i already deleted that file). and now i see the prob thx. what i thought it was some kind of class prob
ty all
Last edited on
would declare it like a popcorn

I have no idea what this is supposed to mean.
I laughed. XD
yeah programmers need laughing and fun
Topic archived. No new replies allowed.