2013-03-04 70 views
2

首先,这是我第一次编写代码,所以我是一个新手。错误'设置'没有在此范围内声明

我正在为使用devkit pro的nd编写代码,所以它全部用C++编写。我想要一个菜单​​,每个菜单屏幕都是空白的,我需要回到上一个菜单。

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

如何在没有获取的情况下执行此操作“错误'设置'未在此范围内声明”。代码:

//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()将得到激活。那么是否有人知道如何正确编码?

在此先感谢。

回答

2

在函数调用的那一刻,你的编译器不知道这个函数的任何内容。有两种方法可以使编译知道您的功能:声明定义

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

void settings(void); 

要解决你的问题,你应该有它的第一个调用之前宣布settings()功能。

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

void settings(); 

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

好文章,从开始,并获得一些额外的细节:Declaration and definition at msdn

+0

感谢您的快速响应;有用! :-) – 2013-03-04 17:21:04

+0

我试图解释这个Mikhahail,但更好的解释,从你:) – OriginalCliche 2013-03-04 21:27:36

0

settings()是局部功能。其定义后只能调用。移动上面的定义controls()或通过头文件使其可用。

0

速战速决将controls()前增加了settings()预先声明,如下所示:

void settings() ; 

完整代码:

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

也看到这一篇主题C++ - Forward declaration

+0

感谢您的反应快,太:-) – 2013-03-04 17:21:28

0

问题是设置()在controls()和控件试图调用settings()后声明的。但是,由于settings()尚不存在,因此无法这样做。

您既可以在controls()之前移动settings()的定义,也可以在controls()之前执行settings()的前向声明。

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

您是否首先在头文件中声明了设置()?另外,我没有看到您将任何方法作为您的类名称或命名空间的范围,因为如果这些方法是在头文件中声明的,您可能会这样做。

如果你不需要头文件,无论出于何种原因,然后改变你写的顺序。在使用它之前定义设置()。

相关问题