C++ 错误:'int' 之前的预期主表达式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8117282/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 18:02:15  来源:igfitidea点击:

C++ error: expected primary-expression before ‘int’

c++compiler-errors

提问by Will Gunn

I am getting this error for every int in this section of code;

这部分代码中的每个 int 都会出现此错误;

if(choice==2) {
    inssort(int *a, int numLines);
}
if(choice==3) {
    bubblesort(int *a, int numLines);
}
if(choice==4) {
    mergesort(int *a, int numLines);
}
if(choice==5) {
    radixsort(int *a, int numLines);
}
if(choice==6) {
    return 0;
}

Thats where I call the functions in main. If you are wondering I am writing a small program that gives the user a choice when sorting a list between 4 different types of sorting algorithms.

这就是我在 main 中调用函数的地方。如果您想知道我正在编写一个小程序,它可以让用户在 4 种不同类型的排序算法之间对列表进行排序时进行选择。

Any help would be appreciated.

任何帮助,将不胜感激。

回答by Mysticial

You can't use the declaration types when you're callingthe functions. Only when you declarethem are they needed:

调用函数时不能使用声明类型。只有当你声明它们时才需要它们:

if(choice==2)
{
    inssort(a, numLines);
}
if(choice==3)
{
    bubblesort(a, numLines);
}
if(choice==4) 
{
    mergesort(a, numLines);
}
if(choice==5) 
{
    radixsort(a, numLines);
}
if(choice==6) 
{
    return 0;
}

回答by zwol

You're using function declarationsyntax to make function calls. That's not necessary, and (as you have discovered) doesn't even work. You can just write

您正在使用函数声明语法进行函数调用。这不是必需的,而且(正如您所发现的)甚至不起作用。你可以写

if (choice == 2)
    inssort(a, numLines);
// etc

By the way, a switchwould be more idiomatic here.

顺便说一句, aswitch在这里会更惯用。

回答by sam

if(choice==2)
{
 inssort(int *a, int numLines);
}

your code turn to this

你的代码转向这个

if(choice==2)
{
 inssort(&a, numLines);
}