理解范围的问题

Problems with understanding scope

本文关键字:问题 范围      更新时间:2023-10-16

i | m在了解类或功能的范围时遇到问题。这个程序是不完整的,但是我无法在同一类中,然后在其他类中使用功能。例如:我遇到一个错误,说

"在此范围中没有声明'selector'

您能帮我弄清楚怎么了吗?谢谢

#include <iostream>
using namespace std;       
int main(void){
selector();
}  
void selector(){
    linkedList test;
    /* block of code */           
 }  
class linkedList{
   Node *head; 
public:
  linkedList(){
    head = NULL;
    }
    //other lines
 };
class Node{
public:
    int data;
    Node * next;
}

我不明白您为什么要谈论类,但是函数的范围是从其声明到文件末尾。只需在您的代码中交换两个函数:

void selector() {
    // linkedList test;
    /* block of code */           
}
int main() {
    selector();  // selector is in scope here
}

(我不确定您为什么要做int main(void)。这更多是C事物。没有参数的C 功能看起来像int main()。)