变量 std::fstream 文件具有初始值设定项,但类型不完整 编译错误

variable std::fstream file has initializer but incomplete type Compilation error

本文关键字:类型 错误 编译 文件 fstream std 变量      更新时间:2023-10-16

这是编译器显示错误位置的文件。我在网上搜索了相同的错误,其中大多数是由于不包括引起的,但在这里我包含了它,它仍然显示此错误。

我忘了补充,在Visual Studio中编译的所有内容都没有问题,但是当我将其上传到我的学校矩阵时,它显示编译错误,并且消息是 "变量 std::fstream 文件具有初始值设定项但类型不完整"。

#include <iostream>
#include <fstream>
#include "MyFile.h"
using namespace std;
using namespace sict;
int main() {
fstream file("ms3.txt", ios::out);
file << "one" << endl << "two" << endl;
file.close();
MyFile F("ms3.txt");
F.load(file);
cout << "Linear: " << F << endl;
cout << "As is: " << endl;
F.print();
cout << "Enter the following: " << endl << "three<ENTER>" << endl <<     "four<ENTER>" << endl << "Ctrl-Z<ENTER>" << endl << endl;
cin >> F;
F.store(file, true);
F.load(file);
cout << F << endl;
F.print();
return 0;
}

这是我的MyFile.h和MyFile.cpp

#ifndef SICT_MYFILE_H__
#define SICT_MYFILE_H__
#include "Streamable.h"
#include "Streamable.h"
#include "Streamable.h" // Streamable.h is included three times on purpose.
namespace sict {
class MyFile : public Streamable {
char fname_[256];
char text_[10000];
public:
MyFile(const char* fname);
std::fstream& store(std::fstream& file, bool addNewLine)const;
std::fstream& load(std::fstream& file);
std::ostream& write(std::ostream& os, bool linear)const;
std::istream& read(std::istream& is);
void print();
};
std::ostream& operator<<(std::ostream& ostr, const MyFile& mf);
std::istream& operator >> (std::istream& istr, MyFile& mf);
}
#endif

我的文件.cpp

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <fstream>
#include <cstring>
#include "MyFile.h"
using namespace std;
namespace sict {
MyFile::MyFile(const char* fname) {
text_[0] = char(0);
strcpy(fname_, fname);
}
fstream& MyFile::store(std::fstream& file, bool addNewLine)const {
file.open(fname_, ios::app | ios::out);
int i = 0;
while (text_[i]) {
file << text_[i];
i++;
}
file.close();
return file;
}
fstream& MyFile::load(std::fstream& file) {
file.open(fname_, ios::in);
int i = 0;
while (!file.fail()) {
text_[i++] = file.get();
}
file.clear();
file.close();
if (i > 0) i--;
text_[i] = 0;
return file;
}
ostream& MyFile::write(std::ostream& os, bool linear)const {
for (int i = 0; text_[i]; i++) {
if (linear && text_[i] == 'n') {
os << " ";
}
else {
os << text_[i];
}
}
return os;
}
istream& MyFile::read(std::istream& is) {
is.getline(text_, 9999, EOF);
return is;
}
void MyFile::print() {
write(cout, false);
cout << endl;
}
std::ostream& operator<<(std::ostream& ostr, const MyFile& mf) {
return mf.write(ostr, true);
}
std::istream& operator >> (std::istream& istr, MyFile& mf) {
return mf.read(istr);
}
}

MyFile.h 和 MyFile.cpp 被给出,所以我不应该编辑任何东西,我还为 Streamable.h 中的 4 个纯虚拟函数添加了一个接口。

#ifndef SICT_STREAMABLE_H__
#define SICT_STREAMABLE_H__
namespace sict {
class Streamable {
public:
virtual std::fstream& store(std::fstream& file, bool addNewLine)const = 0;
virtual std::fstream& load(std::fstream& file) = 0;
virtual std::ostream& write(std::ostream& os, bool linear)const = 0;
virtual std::istream& read(std::istream& is) = 0;
};
}
#endif

这完全没有意义:#include "Streamable.h" // Streamable.h is included three times on purpose.事实上,除了浪费编译时间之外,它什么也做不了,因为无论如何你都可以防止#ifndef SICT_STREAMABLE_H__的多个包含。

此外,一个好习惯是始终首先包含匹配的头文件(仅在预编译头文件之后应用(。这样,您就知道像MyFile.h这样的标头具有为任何源文件编译的任何必要包含(前提是您不做奇怪的事情(。

因此,MyFile.cpp应该从以下方面开始:

#include "MyFile.h"