变量 std::fstream 文件具有初始值设定项,但类型不完整 编译错误
variable std::fstream file has initializer but incomplete type Compilation error
这是编译器显示错误位置的文件。我在网上搜索了相同的错误,其中大多数是由于不包括
我忘了补充,在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"
相关文章:
- 类没有命名C++代码中的类型错误
- 不完整类型错误(E0409、E0070、E0515)
- 使用具有结构不完整类型错误的模板
- 不命名构造函数和析构函数上的类型错误
- 返回派生类型时出现协变类型错误
- 为什么此构造函数没有给出不完整的类型错误?
- 定义模板化结构的特征时出现不完整的类型错误
- 使用重载构造函数时出现不完整的类型错误
- 为什么我得到表达式必须有类类型错误?
- 输出 [left]=input[i] 行中的 c++ 代码中存在无效的类型错误
- 枚举成员不是类型错误
- 如何修复"ctypes"。参数错误:参数 2:<键入"异常.类型错误">:RaspberryPi 中的错误类型"错误
- 外部 "C" Visual Studio 2015 中的显式类型错误,DLL 测试代码
- 非默认析构函数会导致不完整的类型错误
- 为什么按引用传入会导致绑定引用类型错误
- Objective-C C++ 包装器 类型错误的不完整定义
- 输入类型错误
- 当构造函数的参数类型错误时引发异常
- 命名空间中的'bad_cast' 'std' 未命名类型错误
- C UNWORKOWN类型错误