我四处搜寻,似乎找不到我在编码项目中收到的错误的答案。我正在尝试创建一个程序,要求用户输入名称,然后搜索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) {
    // ...
}

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

关于c++ - C++: “error: expected ' ,' or ' …' before ' (' token”,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36107975/

10-15 03:18