创建一个函数,打开一个文件并使用ifstream和ofstream读取它,但它的QT表示我没有声明该函数



试图创建一个函数,该函数使用ifstream和offstream打开文件,但QT编译器告诉我的变量没有声明。程序要长很多,但它的这个特定部分会导致错误。

我需要使函数接收主代码中声明的变量,以便以后能够打开文件。不能使用全局变量。我可以在不使用函数的情况下运行程序,但这是必须的。

/home/eip/Desktop/atm/AMTFINAL/main.cpp:18:错误:变量或字段"OpenUserFile"声明为无效

/home/eip/Desktop/atm/AMTFINAL/main.cpp:18:错误:未在此范围中声明"load">

/home/eip/Desktop/atm/AMTFINAL/main.cpp:18:错误:应在","标记之前使用主表达式void OpenUserFile(加载&、ReceiptCreator&和保存&(^/home/eip/Desktop/atm/AMTFINAL/main.cpp:18:错误:未在此作用域中声明"ReceiptCreator"void OpenUserFile(加载&、ReceiptCreator&和保存&(^//////////
/home/eip/Desktop/atm/AMTFINAL/main.cpp:18:错误:应在","标记之前使用主表达式void OpenUserFile(加载&、ReceiptCreator&和保存&(^/////////////home/eip/Desktop/atm/AMTFINAL/main.cpp:18:错误:未在此作用域中声明"Save"void OpenUserFile(加载&、ReceiptCreator&和保存&(^//////////home/eip/Desktop/atm/AMTFINAL/main.cpp:18:错误:应在"("标记之前使用主表达式void OpenUserFile(加载&、ReceiptCreator&和保存&(^

#include<iostream>
#include<iomanip>
#include<fstream>
#include<cstdlib>
#include<ctime>
using namespace std;



void OpenUserFile(load&, ReceiptCreator&,Save&){
load.open("id.txt"); // reads id.txt.

if(load.fail()){ //If file has problems, user receives an error message
cout<< "Error reading user id" <<endl;
}
ofstream& ReceiptCreator,Save; // instruction to create a file.
ReceiptCreator.open("receipt.txt", ios::app); //prints out output to external .txt file.
if(ReceiptCreator.fail()){ //if error reading display, error message
cout<< "Error printing transaction receipt" <<endl;
}
}
int main(){
time_t C = time(0); // uses computers curren time
string CT = ctime(&C);// shows user current time

string account, userName, SocialSec, PassNum, UserBalance,Bankname, FirstName;
string LastName, AccountNum, SocialNum;//

int PassFile, PassUser,WithDrawal; // variables for user password
double InitialBalance,Deposit, CurrentBalance;// variables for aritmethic calculations
unsigned ATMNUM = 100+rand()%500; // randomizes atm number
int receipt,process, UI = -99, retry = 1; //
ofstream ReceiptCreator,Save;
ifstream load; // starts process to read user information.
int OpenUserFile(load&, ReceiptCreator&,Save&);
cout << "Loading data from file..." << endl;

void foo(int x){}是一个带有一个类型为int的参数的函数。

void foo(int){}是具有一个类型为int的未命名参数的函数。

void OpenUserFile(load&, ReceiptCreator&,Save&){}是一个具有类型为load&ReceiptCreateor&Save&的未命名参数的函数。但是,在您的代码中没有这些类型。我交给你来弄清楚正确的签名。

您必须逐个修复错误,因为一个错误可能会混淆编译器并引发其他错误。我现在能发现的另一个问题是

ofstream& ReceiptCreator,Save;

不能不初始化引用。我想这一行只是修复代码的尝试,因为名称是参数的名称,然后你可以删除那一行。如果没有,则需要决定是将流作为参数传递,还是仅在函数内部传递,而不是两者都传递。

最新更新