【问题标题】:Cant call int main to repeat program c++不能调用 int main 来重复程序 c++
【发布时间】:2015-01-28 07:22:27
【问题描述】:

由于某种原因,我的程序不会让我调用 main。我试图让程序重复,这样我就可以继续在 main 中添加东西,但它会假装 main 不存在并跳过它。

这是我的代码,我逐行跟踪它,即使我输入正确的信息,它也只是拒绝读取 int main();有什么想法吗?

编辑:我是个白痴。感谢您的帮助!

#include <iostream>

using namespace std;

,,,

int main()
{
    // New OrderedList
    OrderedList OrderedList;//no constructor called - is head initialized to NULL?
    char repeat;

    int choice = 0, data;

    cout << "Choose from the following menu options,\n"
         << "1: Add an item\n"
         << "2: Search for an item\n"
         << "3: Delete an item\n"
         << "4: Display the list\n"
         << "5: Destroy the list\n";
    cin >> choice;

        if (choice <= 3)
    {
        cout << "\nPlease enter the item.";
        cin >> data; 
    }
    switch(choice)
    {
    case 1:
        OrderedList.Insert(data);
        break;
    case 2: 
        OrderedList.Search(data);
        break;
    case 3: 
        OrderedList.Delete(data);
        break;
    case 4:
        OrderedList.Print();
        break;
    case 5:
        //delete OrderedList; "no constructor called - is head initialized to NULL?"
        break;
    }

    cout << "Repeat Y/N?\n";
    cin >> repeat;

    if (repeat == 'y' || repeat == 'Y')
    int main();
    return 0;
}

【问题讨论】:

  • (1) 你没有调用main,你声明是:int main();main()。您应该创建另一个函数。
  • 调用main时删除“int”关键字。
  • 本帖讨论调用main():stackoverflow.com/questions/4518598/…
  • 请勿拨打main,因为这是非法的。相反,请创建自己的 Main 并调用它。另外,避免不必要的递归,因为堆栈会沿着这条路径爆炸。

标签: c++ language-lawyer


【解决方案1】:

不要调用main。相反,请使用以下 1 班轮

int main(int argc, char* argv[]) { 
   return (myMain(argc, char* argv[]);) 
}

使用你自己的 main ... 可以递归调用。

int myMain(int argc, char* argv[]) 
{
   // what ever you want, including recursion, 
   // but do not call what you are using for "int main(int, char**)"
}

但是,现在您应该能够看到“main”对您的所有同行都具有特殊含义,因此在“main”上使用任何变体只会造成混淆。

示例:在我的文件“dumy142.cc”中,我更喜欢使用(有点)更有意义的名称,例如“int t142(int argc, char* argv[])”:

int main(int argc, char* argv[]) {
   return(t142(arc, argv);
}

【讨论】:

    【解决方案2】:

    您不应该在应用程序中调用main 函数,而是可以尝试使用while 循环。

    int main(){    
       char repeat = 'Y';
    
       while( repeat == 'y' || repeat == 'Y' )    
       {
          //do some stuff
    
          cout << "Repeat? Y/N" << endl;
          cin >> repeat;    
       }
    
       return 0; 
    }
    

    祝你好运!

    【讨论】:

      【解决方案3】:

      你不应该递归调用main(),事实上,你根本不应该调用它。事实上,C++ 标准禁止在“可能评估的表达式”中使用 main 调用,参见标准中的§3.6.1.3(正如@Captain Obvlious 所指出的那样)。 main() 是您程序的 C++ 启动函数,在执行所有其他(可能的)初始化之后由运行时库调用。

      如果您想重复 main() 中的内容,只需使用 while(或 for)循环即可。

      PS:以下似乎有效:

      使用静态变量作为停止条件,并递归调用main(),但是我不得不说我从未见过这种做法,我根本不会推荐它。刚刚测试过这行得通:

      #include <iostream>
      using namespace std;
      
      // calling main() 10 times, recursively
      int main() {
          static int i = 10; 
          cout << "Hello, World!" << endl; 
      
          i--;
          if(i>0)
              main();
      }
      

      但是,如果您使用-pedantic-pedantic-errors 进行编译,您将分别收到警告/错误,说明您不应该/不能调用main()

      【讨论】:

      • 好吧,其实你可以在我们的代码中调用main。不过,不推荐=)
      • 当然,你可以做的是创建另一个函数,如'int mainAux(int argc, char ** argv)',将所有代码移到那里,然后从 main 调用该函数()(或来自 mainAux()),随心所欲。
      • @Anton 谢谢,在我自己测试了一个段错误之后,实际上只是在编辑帖子:)。
      • 供参考 - §3.6.1.3 - 函数 main 不得在程序中使用。
      • @CaptainObvlious 谢谢,这很有趣。即使-Wall -Wextrag++ 飞过代码...
      【解决方案4】:

      如果换行

      int main();
      

      (这是一个函数声明)到

      main();
      

      这将是一个函数调用,它可能会起作用。但是,最好将 main 的内容包装在一个循环中。

      编辑:我刚刚检查了标准,有趣的是,对 main 的递归调用仅在 C++ 中是非法的。我觉得这很令人惊讶。

      编辑 2:哦,问题标记为 C++。在这种情况下,您将被禁止递归调用main!恶魔会从你的鼻子里出来等等。

      【讨论】:

      • AFAIK C 允许您调用main,无论它是否递归。在 C 中是否允许它是无关紧要的,因为问题被标记为 C++。
      • @CaptainObvlious 刚刚检查了标准,你是对的,但奇怪的是 g++ 至少没有发出警告,在程序编译(并运行)时就很好
      • 在初始化像int a = main();这样的全局变量时怎么样。这不是递归的:)
      • 实际上,递归并不是程序格式错误所必需的。标准规定“函数main 不得在程序中使用(3.2)。”在 3.6.1 (3) 中。
      • @CaptainObvlious 纠正了我的回答,你是对的
      猜你喜欢
      • 1970-01-01
      • 2018-03-16
      • 1970-01-01
      • 2017-10-16
      • 2013-11-04
      • 2011-07-08
      • 2011-06-18
      • 2014-10-09
      • 2012-11-12
      相关资源
      最近更新 更多