Question

first of all, this is one of my first times writing code so I'm a newb.

I'm writing for the nds using devkit pro, and so its all written in c++. I want to have a menu, each menu screen is a void, and I need to have a way to go back to the previous menu.

Also, I made sure that on the actual code, there is NO syntax errors (unless not being declared in this scope is considered a syntax error).

How can I do this without getting the "Error 'settings' was not declared in this scope". Code:

    //Headers go here

    void controls()
    {
                                 //Inits and what not go here
            if (key_press & key_down) 

    /*This is generally how you say if the down key has been pressed (This syntax might be wrong, but ignore that part)*/
            {
            settings(); //This part doesn't work because it can't read back in the code
            }

    }
    void settings()
    {
                                 //Inits and what not go here
            if (key_press & key_down) 
            {
            controls();
            }

    }
    void mainMenu()
    {
                 //Inits and what not go here
            if (key_press & key_down) 
            {
                    settings();
            }
    }

AND NOTE, somewhere outside of this code, mainMenu() would get activated. So does anyone know how to code this correctly?

Thanks in advance.

Was it helpful?

Solution

At the moment of your function call, your compiler don't know anything about this function. There is two ways to make the compile aware of your function: declaration and definition.

To declare the function you have to put the function synopsis (function arguments and return values) to the top of the compilation module like this.

void settings(void);

To fix your problem you should have the settings() function declared before the first call of it.

In your case you should probably declare the function at the top of the file. By this way compiler will be aware about the function and the arguments that should be passed into.

void settings();

void controls()
{
...
}
void settings()
{
...
}
void mainMenu()
{
...
}

Good article to start from and to get some additional details: Declaration and definition at msdn

OTHER TIPS

The quick fix would be to add a forward declaration for settings() before controls() like so:

void settings() ;

Complete code:

//Headers go here

void settings() ;

void controls()
{
                             //Inits and what not go here
        if (key_press & key_down) 

/*This is generally how you say if the down key has been pressed (This syntax might be wrong, but ignore that part)*/
        {
        settings(); //This part doesn't work because it can't read back in the code
        }

}
void settings()
{
                             //Inits and what not go here
        if (key_press & key_down) 
        {
        controls();
        }

}
void mainMenu()
{
             //Inits and what not go here
        if (key_press & key_down) 
        {
                settings();
        }
}

Also see this previous thread C++ - Forward declaration

settings() is a local function. It can be called only after its definition. Move the definition above controls() or make it available via header file.

The problem is that settings() is declared after controls() and controls is trying to call settings(). However, since settings() doesn't exist yet, it's unable to do so.

You can either move the definition of settings() before that of controls() OR you can do a forward declaration of settings() before controls().

void settings(); //forward declaration
void controls() { 
  .....
}
void settings() {
  .... 
}

Have you declared settings() in your header file first? Also I don't see you scoping any of your methods to your class name or namespace as you probably would if these methods were declared in a header file.

If you do NOT need a header file, for whichever reason, then change the order you write. Define settings() before you use it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top