首先,这是我第一次写代码,所以我是新手。
我正在使用 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/