C++:"error: expected ',' or '...' before '(' token"
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);
,然后从定义中删除默认值。
现场示例
相关文章:
- Arduino无法编译名为SP:"Expected ')' before '(' token"的变量
- 为什么我的功能在使用 goto 时会给我带来"expected primary-expression before '}' token"?
- 使用结构和数组时的"expected primary expression before '.' token"
- 如何解决"Expected expression before " = " token"?
- 错误:创建模板类时"expected unqualified-id before '{' token"
- 如何解决"expected constructor, destructor, or type conversion before ‘(’ token"错误?
- 这个编译器错误是什么意思 - "qualified-id in declaration before ‘=’ token" C++?
- 获取链表上的"expected ‘)’ before ‘&’ token"到 BST 构造函数签名
- 使用宏访问指向数据成员的指针会导致"error: expected unqualified-id before ‘*’ token"
- "error: expected primary-expression before '<=' token" 我做错了什么?
- 内存集调用位置的"error: expected constructor, destructor, or type conversion before '(' token"
- 设置if流,获取'error: expected initializer before ‘.’ token'
- 在2D伊辛模型上工作。C++新手。我在第 23 行收到错误"expected unqualified-id before '{' token"
- 我的代码看起来不错。为什么我要得到"error: expected primary-expression before ')' token"?
- 如何修复"Expected Primary-expression before ')' token"错误?
- C++错误"tabulate.cc:19: error: expected primary-expression before ',' token"
- 使用多个文件时"array bound is not an integer constant before ']' token"
- "expected initializer before '<' token"尝试模板成员专用化
- 为什么海湾合作委员会说"a function-definition is not allowed here before '{' token"?
- C++:"error: expected ',' or '...' before '(' token"