简体   繁体   English

为什么我的函数收到的错误是它未在此范围内定义? 我只是试图调用该函数并让它现在运行

[英]Why is my function receiving an error that it is not defined in this scope? I am simply trying to call the function and have it run as of right now

I am simply trying to have the createList function called and run, but I get an error that create list was not defined in this scope. 我只是尝试调用并运行createList函数,但是我得到一个错误,即创建列表未在此范围内定义。 I am not sure what I am doing wrong. 我不确定我做错了什么。

typedef struct state {
  char trans[100];
  bool final;
  struct state *next;
} STATE;

STATE *stu = NULL;

stu = createList(stu, trans, states);

STATE* createList (STATE *stu, char trans, int states) {
    for (int i = states; i > 0; i--) {
        printf("%d", i); /*ccode check*/
    }
    return stu;
}

If this is your C file, then there are two things to note: 如果这是您的C文件,那么有两点需要注意:

  1. You must declare the function (ie a prototype) before you can use it. 您必须先声明该函数(即原型), 然后才能使用它。

  2. You cannot have executable code on the global level. 您无法在全局级别拥有可执行代码。

Try: 尝试:

typedef struct state {
  char trans[100];
  bool final;
  struct state *next;
} STATE;

STATE *stu = NULL;

STATE* createList (STATE *stu, char trans, int states) {
    for (int i = states; i > 0; i--) {
        printf("%d", i); /*ccode check*/
    }
    return stu;
}

int main (void)
{
    stu = createList(stu, trans, states);
    return 0;
}

Note that I didn't give a prototype in this example because the complete function is defined before it's used, so the compiler knows all about it. 请注意,我没有在这个例子中给出原型,因为在使用之前定义了完整的函数,因此编译器知道它的全部内容。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我有一个返回结构的char *成员的函数。 我正在尝试调用该函数并为其分配一个字符串,但出现错误 - I have a function that's returning a char* member of a struct. I am trying to call that function and assign a string to it, but am getting an error 我正在尝试使用 C 中的递归来反转链表,我对我的递归 function 有一些疑问 - I am trying to reverse a linked list using recursion in C, I have some doubts on my recursive function 为什么我在尝试运行 find function 即使正确访问 memory 时遇到分段错误? - Why I am getting segmentation fault when trying to run the find function even though accessing memory properly? 我试图将数组传递给我的函数,但输出中没有显示,为什么? - I am trying to pass an array to my function but nothing shows up on the output, why? 我正在尝试在 C 中实现我自己的 strpbrk function - I am trying to implent my own strpbrk function in C 我想制作自己的 (strlen) function 版本...但是为什么我会出错 - I want to make my own version of (strlen) function...but why i am getting error 为什么在尝试将指向数组的指针作为函数的参数时出现访问冲突错误? - Why I have access violation error when trying to put a pointer to an array as parameter of a function? 我正在尝试使用双指针将字符串传递给 function 但出现错误 - I am trying to pass a string into a function with a double pointer but getting an error 为什么我收到一条错误消息,提示我的 function 类型冲突? (已编辑) - Why am I getting an error saying that my function type is conflicting? (edited) 为什么在“ for”循环中收到一条错误消息,说“ break”语句不在循环或切换中? - Why am I receiving an error at my “for” loop saying 'break' statement not in loop or switch?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM