C++:"error: expected ',' or '...' before '(' token"



我四处搜索,似乎找不到我在编码项目中收到的错误的答案。 我正在尝试创建一个程序,要求用户输入一个名字,然后搜索2012年最受欢迎的婴儿名字,以找到当年的名字有多普遍。 但是,尽管这似乎是一个很常见的问题,但在定义一个我无法弄清楚的函数时,我遇到了一个问题。 这是到目前为止的代码:


/*Description: The code below asks the user to input a baby name and then
finds the popularity ranking of that name for both boys and girls in the
year 2012.
*/
// INCLUDE DIRECTIVES
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
// FUNCTION DECLARATIONS
std::string nameGet(std::string& userName);
/*PRECONDITION: n.a.
POSTCONDITION: Outputs the name provided by the user*/

int findPosition(std::string userName, int namePosition(0));
/*PRECONDITION: Takes a string variable
POSTCONDITION: Outputs the ranking number of said string variable within
the 2012 list of popular baby names*/


// Main Function
int main()
/*PRECONDITION: n.a.
//POSTCONDITION: Popularity ranking of name according to list of popular 21012
baby names*/
{
    // Local Variables
    std::string userName;
    int boyNamePlace(0), girlNamePlace(0);
    nameGet(userName);
    std::cout << std::endl << userName << std::endl;
    boyNamePlace = findPosition(userName, boyNamePlace);
    girlNamePlace = findPosition(userName, girlNamePlace);

    return EXIT_SUCCESS;
}

// FUNCTION DEFINITIONS
std::string nameGet(std::string& userName){
/*PRECONDITION: n.a.
POSTCONDITION: Outputs the name provided by the user*/
    std::cout << "Enter name (capitalize first letter): ";
    std::cin >> userName;
    return userName;
}

int findPosition(std::string userName, int namePosition(0)){
/*PRECONDITION: Takes a string variable
POSTCONDITION: Outputs the ranking number of said string variable within
the 2012 list of popular baby names*/
    // Local Variables
    std::ifstream babyNames;
    bool nameFound(false);
    //Opens the .txt file
    babyNames.open("babynames2012.txt");
    if (babyNames.fail())
    {
        std::cout << "I/O Stream failure when attempting to open file.";
        return EXIT_FAILURE;
    }
    else
    {
        std::cout << "Success";
    }

    for(namePosition = 0; nameFound == false; namePosition++)
    {

        return 0;
    }
    return namePosition;
}

如您所见,这仍然是一个正在进行的工作,其中有许多 cout 语句,以便检查程序在编译后将运行多远而没有任何错误。 标题中提到的错误消息同时出现在 int 函数 "findPosition" 的声明和定义中。

我还不知道如何运行调试器,这是我第一次发布,所以如果格式有点不对劲,我很抱歉。

是这一行:

int findPosition(std::string userName, int namePosition(0));

您是否正在尝试为该参数设置默认值?如果是这样,正确的方法是这样的:

// Declaration
int findPosition(std::string userName, int namePosition = 0);
// Definition
int findPosition(std::string userName, int namePosition) {
    // ...
}

如果您尝试做其他事情,请告诉我,我会相应地更新我的答案。

您应该将声明更改为:

int findPosition(std::string userName, int namePosition = 0);

,然后从定义中删除默认值。

现场示例

相关内容

最新更新