在为 class 创建 .h 和 .cpp 文件后如何删除错误? C++
How do I remove errors after creating .h and .cpp files for a class? C++
所以我正在学习在我的程序中使用 class .h 和 .cpp 文件来读取包含银行帐户信息的文件。最初代码运行良好,但是在创建 .h 和 .cpp class 文件后,事情不再那么顺利,因为我遇到了对我来说没有意义的奇怪错误。
这是我的主要 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&'|
我认为这可能是 headers 的问题?我有点疯狂,把我所有的相关 headers 放在每个文件上,试图让它工作。
using namespace std;
这被认为是 a bad programming practice,如果您忘记了这实际上是 C++ 语言的一部分,您将帮自己一个忙。尽管在某些适当的情况下会使用 using namespace
,但在对 C++ 及其结构和语法有更好的技术理解之前,应该避免这种情况;为了识别和理解何时可以正确使用它(如果有的话)。
在你的 main() 中你有:
string fileName;
在名为string
的C++库中没有这样的class。 class的正确名称是std::string
;然而,通过将 using namespace std;
推到上面的几行,你最终幸福地没有意识到这个基本的、基本的事实。
现在,理解了这些之后,让我们回过头来看看你的头文件:
ifstream sourceFile;
嗯,在名为 ifstream
的 C++ 库中也没有这样的 class。 class的专有名称是std::ifstream
。 C++ 库中的所有 classes 和模板都存在于 std
命名空间中。
但是,因为当您 #include
d 头文件时您的 using namespace std;
别名尚未定义,您的编译器无法识别 class 名称,并且您得到此编译错误作为奖励。
解决方法是不要在头文件中塞入 using namespace std;
。这只会导致更多的混乱和混乱。正确的修复是:
从您的代码中完全删除 using namespace std;
。
使用 C++ 库中所有 classes 的全名,无处不在。将所有对 string
、ifstream
和其他所有内容的引用替换为它们的实际 class 名称:std::string
、std::ifstream
等。养成每次都显式使用 std
命名空间前缀的习惯。一开始可能觉得很麻烦,但很快你就会养成这个习惯,而且你不会再想了。
您再也不会被这些编译错误搞糊涂了。
所以我正在学习在我的程序中使用 class .h 和 .cpp 文件来读取包含银行帐户信息的文件。最初代码运行良好,但是在创建 .h 和 .cpp class 文件后,事情不再那么顺利,因为我遇到了对我来说没有意义的奇怪错误。
这是我的主要 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&'|
我认为这可能是 headers 的问题?我有点疯狂,把我所有的相关 headers 放在每个文件上,试图让它工作。
using namespace std;
这被认为是 a bad programming practice,如果您忘记了这实际上是 C++ 语言的一部分,您将帮自己一个忙。尽管在某些适当的情况下会使用 using namespace
,但在对 C++ 及其结构和语法有更好的技术理解之前,应该避免这种情况;为了识别和理解何时可以正确使用它(如果有的话)。
在你的 main() 中你有:
string fileName;
在名为string
的C++库中没有这样的class。 class的正确名称是std::string
;然而,通过将 using namespace std;
推到上面的几行,你最终幸福地没有意识到这个基本的、基本的事实。
现在,理解了这些之后,让我们回过头来看看你的头文件:
ifstream sourceFile;
嗯,在名为 ifstream
的 C++ 库中也没有这样的 class。 class的专有名称是std::ifstream
。 C++ 库中的所有 classes 和模板都存在于 std
命名空间中。
但是,因为当您 #include
d 头文件时您的 using namespace std;
别名尚未定义,您的编译器无法识别 class 名称,并且您得到此编译错误作为奖励。
解决方法是不要在头文件中塞入 using namespace std;
。这只会导致更多的混乱和混乱。正确的修复是:
从您的代码中完全删除
using namespace std;
。使用 C++ 库中所有 classes 的全名,无处不在。将所有对
string
、ifstream
和其他所有内容的引用替换为它们的实际 class 名称:std::string
、std::ifstream
等。养成每次都显式使用std
命名空间前缀的习惯。一开始可能觉得很麻烦,但很快你就会养成这个习惯,而且你不会再想了。
您再也不会被这些编译错误搞糊涂了。