【问题标题】:variable 'fstream grabpass' has initializer but incomplete type变量“fstream grabpass”具有初始化程序但类型不完整
【发布时间】:2010-11-26 18:42:39
【问题描述】:

我在第 37 行使用 fstream 从文件中读取错误 (fstream grabpass("passwords.txt");) 但我似乎没有做错任何事情。

#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(string username, string password)
{
    if (username=="test"/*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);

    fstream grabpass("passwords.txt");
    getline(grabpass,storedPassword);
    grabpass.close();
    login(username,password);

    return 0;
}

【问题讨论】:

    标签: c++


    【解决方案1】:

    您需要添加#include &lt;fstream&gt;。猜测一下,&lt;iostream&gt; 可能包含fstream 的声明(最类似于通过&lt;iosfwd&gt;),但不是定义,因此当您尝试定义该类型的对象时,它的类型不完整。

    【讨论】:

    • 呃。我为什么不这样做……谢谢,杰瑞。我犯了一个愚蠢的错误。
    猜你喜欢
    • 1970-01-01
    • 2012-04-03
    • 1970-01-01
    • 1970-01-01
    • 2018-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多