c++ - 错误 'settings' 未在此范围内声明

标签 c++ compiler-errors void devkitpro

首先,这是我第一次写代码,所以我是新手。

我正在使用 devkit pro 为 nds 写作,所以它都是用 C++ 编写的。我想要一个菜单​​,每个菜单屏幕都是空的,我需要有一种方法可以返回到上一个菜单。

此外,我确保在实际代码中没有语法错误(除非未在此范围内声明被视为语法错误)。

如何在不收到“错误‘settings’未在此范围内声明”的情况下执行此操作。代码:

    //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();
            }
    }

并且注意,在此代码之外的某处,mainMenu() 将被激活。那么有人知道如何正确编码吗?

提前致谢。

最佳答案

在你调用函数的那一刻,你的编译器对这个函数一无所知。有两种方法可以让编译器识别您的函数:声明定义

要声明函数,您必须像这样将函数概要(函数参数和返回值)放在编译模块的顶部。

void settings(void);

要解决您的问题,您应该在第一次调用它之前声明 settings() 函数。

在您的情况下,您可能应该在文件顶部声明该函数。通过这种方式,编译器将知道应该传递给函数和参数。

void settings();

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

很好的文章,可以作为起点并获得一些额外的细节:Declaration and definition at msdn

关于c++ - 错误 'settings' 未在此范围内声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15206818/

相关文章:

mysql - 将外键添加到现有列时出错

c++ - "explicit qualification in declaration"错误信息是什么意思?

C题: single dereference on a void** double indirection pointer

iphone - 如何调用具有多个参数的方法

scala - 我应该使用 Unit 还是忽略 scala 方法的返回类型?

c++ - 定义所有关系运算符的更短代码

c++ - 如何在 C++ 中获取指向 main() 方法的函数指针?

c++ - 非静态成员作为非静态成员函数的默认参数

c++ - 使用超过 50 个 OR ( || ) 优化 if 语句

oracle - 如何在 sqlplus 中显示错误