为什么海湾合作委员会会说"在此之前不允许使用功能定义' {'令牌&#34 ;?

时间:2016-05-05 15:10:52

标签: c++ compiler-errors

为什么这段代码没有编译?

#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

2 个答案:

答案 0 :(得分:2)

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

#include <iostream>
#include <string>

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

答案 1 :(得分:1)

你忘记了:

1)入口点

2)包括陈述

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

4)cin之前的命名空间

总的来说这看起来不错,但至少应该为你编译。

#include <string>
#include <iostream>

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