【问题标题】:Why does GCC say "a function-definition is not allowed here before '{' token"? [closed]为什么 GCC 说“在 '{' 标记之前不允许函数定义”? [关闭]
【发布时间】:2016-08-31 10:48:37
【问题描述】:

为什么这段代码不能编译?

#include <iostream>

using namespace std;

#define $_ cout << "Use '$_' befor commands to execute them";
#define $_Help cout << "Type $_ befor each command to execute it, type the command without $_ to view information about the command. Commands: Help, Quiz";
#define $_Quiz int quiz(){int score = 0; std::string name; std::string quest1; cout << "What is your name? "; cin >> name; cout << "Hello, " << name << endl; score++; cout << "Is pi > 3.14159? yes or no? "; cin >> quest1; if (quest1 == "yes"){cout << "Correct!"; score++;}else if (quest1 == "no"){cout << "Incorrect, pi has more numbers that just 3.14159.";}cout << " Your score is, " << score << endl; return 0;};
#define Help cout << "Display all of the commands";
#define Quiz cout << "A simple quiz(only one question right now)";
#define testForInputComm if(termComms == "$_Help"){$_Help;}else if(termComms == "$_Quiz"){$_Quiz;}else if(termComms == "Help"){Help;}else if(termComms == "Quiz"){Quiz;};

int main() {
    while(true)
    {
         std::string termComms;
         std::cin >> termComms;
         testForInputComm;
         return 0;
    }
}

int initiation()
{
    cout << "Type a command!";
    cout << "Type: $_Help to view all of the commands";
    return 0;
}

错误信息是:

main.cpp: In function 'int main()':                                                                                                                              
main.cpp:7:26: error: a function-definition is not allowed here before '{' token                                                                                 
 #define $_Quiz int quiz(){int score = 0; std::string name; std::string quest1; cout << "What is your name? "; cin >> name; cout << "Hello, " << name << endl; sc
                          ^                                                                                                                                      
main.cpp:10:91: note: in expansion of macro '$_Quiz'                                                                                                             
 #define testForInputComm if(termComms == "$_Help"){$_Help;}else if(termComms == "$_Quiz"){$_Quiz;}else if(termComms == "Help"){Help;}else if(termComms == "Quiz"
                                                                                           ^                                                                     
main.cpp:17:10: note: in expansion of macro 'testForInputComm'                                                                                                   
          testForInputComm;                                                                                                                                      
          ^                                                                                                                                                      
main.cpp:27:1: error: expected '}' at end of input                                                                                                               
 }                                                                                                                                                               
 ^                                                                                                                                                               
main.cpp:27:1: error: expected '}' at end of input                                                                                                               
main.cpp:27:1: error: expected '}' at end of input

【问题讨论】:

  • @BenjaminLindley 恐怕实际上是one
  • Endless loop in C/C++的可能重复
  • 只是一个想法,但你的main呢?
  • 对不起,我之前的评论很粗鲁。我只是对我所看到的感到震惊。尽管如此,精神依然存在。作为一般规则,如果您可以通过其他任何方式执行此操作,请不要使用宏。

标签: c++ compiler-errors


【解决方案1】:

您需要将该代码嵌入到 main() 函数中:

#include <iostream>
#include <string>

int main() {
    while(true)
    {
         std::string termComms;
         std::cin >> termComms;
         // testForInputComm;
    }
}

【讨论】:

  • 谢谢!不幸的是,当我这样做时,它带来了更多错误。我知道如何修复它们,但你想看看它们和其余代码吗?
  • @xoorath 关于您的编辑:main 不需要返回语句。将生成一个隐含的return 0;
  • @Surge12 "...但是你想看看它们和其余的代码吗?"可能不会。
【解决方案2】:

你忘了:

1) 入口点

2) 包含语句

3) 变量声明之前您正在使用变量。

4) cin 之前的命名空间

总的来说,这看起来是个坏主意,但至少应该为您编译。

#include <string>
#include <iostream>

int main() {
    while(true)
    {
        std::string termComms;
        std::cin >> termComms;
        // use termComms here
    }
    return 0;
}

【讨论】:

  • 我有一些错误要修复...
  • 您的代码缺少cin、@Surge12 上的命名空间。当然,一定要修复你的一些错误。
  • @πάντα ῥεῖ:在堆栈中溢出无限的智慧,如果没有 50 个代表,我无法回复您的评论。所以是的:我不知道你可以省略那里的回报,谢谢。对我来说看起来很恶心,但嘿 - 对每个人来说都是如此。
  • 是的,你必须修复你的代码。我建议在尝试将其粘贴到那里并希望它有效之前学习 C++。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-15
  • 1970-01-01
  • 2012-12-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多