[英]C++: “error: expected ',' or '…' before '(' token”
I've searched around and can't seem to find an answer to an error I'm receiving on a coding project. 我一直在搜索,似乎无法找到我在编码项目上收到的错误的答案。 I'm trying to create a program that asks a user to enter a name and will then search through the most popular baby names in 2012 to find how common a name it was that year. 我正在尝试创建一个程序,要求用户输入一个名称,然后在2012年搜索最受欢迎的宝贝名称,以查找当年的名称有多常见。 However, even though it seems like a pretty common problem, I've run into a problem when defining one of my functions that I can't figure out. 然而,尽管它似乎是一个非常常见的问题,但在定义我无法弄清楚的一个函数时遇到了问题。 Here's the code so far: 这是迄今为止的代码:
/*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;
}
As you can see, this is still a work in progress, with many cout statements throughout in order to check how far the program will operate without any errors once it compiles. 正如您所看到的,这仍然是一项正在进行中的工作,其中包含许多cout语句,以便在编译后检查程序运行的程度而没有任何错误。 The error message mentioned in the title appears both in the declaration and the definition of the int function "findPosition". 标题中提到的错误消息同时出现在声明和int函数“findPosition”的定义中。
I don't know how to run a debugger yet and this is my first time posting, so I'm sorry if the formatting is a bit off. 我不知道如何运行调试器,这是我第一次发布,所以如果格式有点偏,我很抱歉。
It is this line: 就是这条线:
int findPosition(std::string userName, int namePosition(0));
Were you trying to set a default value for that parameter? 您是否尝试为该参数设置默认值? If so the correct way of doing it is this: 如果是这样,正确的做法是这样的:
// Declaration
int findPosition(std::string userName, int namePosition = 0);
// Definition
int findPosition(std::string userName, int namePosition) {
// ...
}
If you were trying to do something else let me know and i will update my answer accordingly. 如果你试图做其他事情让我知道,我会相应地更新我的答案。
You should change the declaration to: 您应该将声明更改为:
int findPosition(std::string userName, int namePosition = 0);
and remove the default value from the definition. 并从定义中删除默认值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.