這是編譯器顯示錯誤位置的文件。我在網上搜索了同樣的錯誤,其中大部分是由於不包括< 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