C++ 错误:“初始化表达式列表被视为复合表达式”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4281904/
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 15:01:24  来源:igfitidea点击:

error: "initializer expression list treated as compound expression"

c++functionparametersdeclaration

提问by Captain Lightning

I'm having an issue compiling the beginnings of a basic password protected file program, I'm getting the above error on line 11, (int login(username,password)). Not sure what's going on here, so it'd be nice if someone could shed some light on the situation.

我在编译基本密码保护文件程序的开头时遇到问题,我在第 11 行收到上述错误 (int login(username,password))。不确定这里发生了什么,所以如果有人能对这种情况有所了解就好了。

#include <iostream>
#include <conio.h>
#include <string>

using namespace std;

int i,passcount,asterisks;
char replace, value, newchar;
string username,password,storedUsername,storedPassword;

int login(username,password);
{
    if (username==storedUsername)
    {
        if (password==storedPassword)
        cout<<"Win!";
        else
        cout<<"Username correct, password incorrect."
    }
    else cout<<"Lose. Wrong username and password.";
}

int main()
{
    cout<<"Username: ";
    cin>>username;
    cout<<"Password: ";
    do
    {
    newchar = getch();
    if (newchar==13)break;
    for (passcount>0;asterisks==passcount;asterisks++)cout<<"*";
    password = password + newchar;
    passcount++;
    } while (passcount!=10);
    ifstream grabpass("passwords.txt")
    grabpass>>storedpass;
    grabpass.close();
    login(username,password);

    return 0;
}

回答by AndersK

int login(username,password);
{

should be

应该

int login(string username,string password)
{

回答by Captain Lightning

You may wan't to fix function declaration

您可能不想修复函数声明

int login(username,password);

Should be changed to

应该改为

int login(const string& username,const string& password);

Also as a style note, you may not want to declare global variable, you can limit scope of most of your variables to local scope in main.

另外作为样式说明,您可能不想声明全局变量,您可以将大多数变量的范围限制在 main 中的局部范围内。

回答by Pavel

You have to specify the data types of username and password.

您必须指定用户名和密码的数据类型。

回答by Maxpm

When declaring a user-defined function with parameters, you must declare the parameter types as well.

声明带参数的用户定义函数时,还必须声明参数类型。

For example:

例如:

int foo(int parameter)
{
    return parameter + 1;
}