变量'fstream grabpass'有初始值设定项但是不完整类型

问题描述:

我在使用fstream的第37行(fstream grabpass(“passwords.txt”);)中看到一个错误读取,但似乎并不像我做错任何事情。变量'fstream grabpass'有初始值设定项但是不完整类型

#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; 
} 

您需要新增#include <fstream>。猜测,<iostream>可能包含声明fstream(大部分类似于通过<iosfwd>),但不是定义,所以当您尝试定义该类型的对象时,它的类型不完整。

+0

呃。我为什么没有这样做......谢谢,杰瑞。我的一个愚蠢的错误。 – 2010-11-26 18:49:08