2017-08-12 35 views
0

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

你應該在這裏發佈完整的錯誤描述。另外這個錯誤可能與「MyFile.h」有關,所以你應該在這裏發佈它。 – VTT

+0

我們只能猜測有兩個'using namespace'會帶來一些衝突的名字。 –

+1

請閱讀如何提供[mcve]! – chtz

回答

0

這沒有任何意義可言:#include "Streamable.h" // Streamable.h is included three times on purpose.事實上,它會爲你避免多次包含有#ifndef SICT_STREAMABLE_H__反正什麼都不做只是浪費編譯時間。

此外,一個好習慣是始終包含匹配的頭文件第一個(僅適用於預編譯頭文件時)。這樣,你知道像MyFile.h這樣的頭文件可以包含任何源文件(只要你不奇怪)編譯。

因此MyFile.cpp應該這樣開始:

#include "MyFile.h" 
相關問題