【发布时间】:2017-03-19 01:22:48
【问题描述】:
因此,我正在学习在我的程序中使用 .h 和 .cpp 类文件来读取包含银行账户信息的文件。最初代码运行良好,但是在创建 .h 和 .cpp 类文件后,事情就不再那么顺利了,因为我遇到了对我来说没有意义的奇怪错误。
这是我的主要 cpp 文件:
#include "Bankaccount.h"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{ string fileName;
cout << "Enter the name of the data file: ";
cin>>fileName;
cout<<endl;
bankAccount object(fileName);
return 0;
}
这是我的 Bankaccount.h 文件
#ifndef BANKACCOUNT_H
#define BANKACCOUNT_H
#include <iostream>
#include <fstream>
#include <string>
class bankAccount
{
public:
bankAccount(string n);
bankAccount();
private:
ifstream sourceFile;
}
最后这是 Bankaccount.cpp 文件
#include "Bankaccount.h"
#include <iostream>
#include <fstream>
#include <string>
bankAccount::bankAccount(string n)
{
sourceFile.open(n.c_str());
}
现在产生这些错误:
include\Bankaccount.h|13|error: expected ')' before 'n'|
include\Bankaccount.h|18|error: 'ifstream' does not name a type|
include\Bankaccount.h|14|note: bankAccount::bankAccount()|
include\Bankaccount.h|14|note: candidate expects 0 arguments, 1 provided|
include\Bankaccount.h|4|note: bankAccount::bankAccount(const bankAccount&)|
include\Bankaccount.h|4|note: no known conversion for argument 1 from 'std::string {aka std::basic_string}' to 'const bankAccount&'|
我认为这可能是标题的问题?我有点疯狂,把我所有的相关标题放在每个文件上,试图让它工作。
【问题讨论】:
-
试试
std::ifstream和std::string。 -
类定义需要一个 ;在右大括号之后。