这是编译器显示错误位置的文件。我在网上搜索了同样的错误,其中大部分是由于不包括< fstream>引起的,但是在这里我已经包含了它,它仍然显示这个错误。变量std :: fstream文件具有初始值设定项,但类型不完整编译错误
我忘了在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
MYFILE.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
你应该在这里发布完整的错误描述。另外这个错误可能与“MyFile.h”有关,所以你应该在这里发布它。 – VTT
我们只能猜测有两个'using namespace'会带来一些冲突的名字。 –
请阅读如何提供[mcve]! – chtz