首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >错误:这里不允许在“{”令牌之前定义函数。

错误:这里不允许在“{”令牌之前定义函数。
EN

Stack Overflow用户
提问于 2022-04-23 11:28:43
回答 1查看 824关注 0票数 -4

我一直在努力解决这个错误。我该怎么解决呢?我试过把函数放在主函数之外,但它仍然不起作用。

代码语言:javascript
运行
复制
int mutexlock = 1, full = 0, emp = 20, x = 0, buffer[100];

void producer();
void consumer();
int randomgenerator();

int main() {

  void producer() {
    int d = randomgenerator();
    buffer[x] = d;
    cout << "\n" << d;
    x++;
  }
  void consumer() {
    x--;
    cout << "\n" << buffer[x] << endl
    cout << "sum" << buffer[x] << (buffer[x] + buffer[x]);
  }
  int randomgenerator() {
    int num = rand() % 6 + 1;
    return (num);
  }
}

错误:

代码语言:javascript
运行
复制
 error: a function-definition is not allowed here before ‘{’ token
 {
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-04-23 11:31:33

你应该把所有的功能都排除在主功能之外。此外,请将程序启动后要运行的代码放在主函数中。

示例:

代码语言:javascript
运行
复制
int main() {
    producer();
    consumer();
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71979212

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档