2016-10-30 6 views
0

myDocument.txtという名前のファイルの内容を読み込んで表示しようとしていますが、私は常にヘッダファイルで次のエラーを受け取ります。は、ヘッダのパラメータを期待しています

./ReadDocument.h:22:24: error: expected parameter declarator std::ifstream myflux("myDocument.txt");

何が起こっているのですか?ここで

は私ReadDocument.hです:

class ReadDocument{ 
public: 
    ReadDocument(); //empty constructor 
    ReadDocument(const std::string& fileName); //constructor 

    void documentContent(); 

    friend std::ostream& operator <<(std::ostream& os, const ReadDocument& d); 

    std::list<std::string> myList; 
    std::ifstream myflux("myDocument.txt"); 

    if(myflux){ 
      //Getting the words from the file string by string 
      std::string data; 
      while(myflux>>data){ 
       myList.push_back(data); 
      } 

      myflux.close(); 
    } 
    else{ 
     std::string message = "\t\tERROR ==> This file doesn't exist."; 
     throw FileDoesntExistException(message); 
    } 

};ここで

は私ReadDocument.cppです:

#include "ReadDocument.h" 

ReadDocument::ReadDocument(){} 

void ReadDocument::documentContent(){} 

std::ostream& operator <<(std::ostream& os, const ReadDocument& d){ 

    for(std::list <std::string> :: const_iterator it = ReadDocument::myList.begin(); it!=ReadDocument::myList.end(); it++) 
    std::cout<<*it<<std::endl; 

    return os; 
} 

そして、これは私は、ファイルと、まだ同じ問題に改名しようとした私のUseReadDocument.cpp

#include "ReadDocument.h" 

int main(){ 
    ReadDocument rd("test.txt"); 
    std::cout<<rd<<std::endl; 

    return 0; 
} 

です。 私はクラスに入れて、何も変更しません。 私はいつも同じエラーが出てきます。 はい、それは私のコード全体

+0

フォーマットを修正してください。 –

+0

あなたはmyFluxを開きますが、myFlux1を閉じます – Chris

+0

それはあなたの 'ReadDocument.h'全体ですか?このエラーの原因となるクラスまたは関数内にないコードを表示しています。 – 1201ProgramAlarm

答えて

1

myfluxの後のクラスの部分は方法ではありません。
コードをdocumentContentのような関数に入れたいですか?


ReadDocument.h:

class ReadDocument{ 
public: 
    ReadDocument(); //empty constructor 
    ReadDocument(const std::string& fileName); //constructor 

    void documentContent(); 

    friend std::ostream& operator <<(std::ostream& os, const ReadDocument& d); 

    std::list<std::string> myList; 
    std::ifstream myflux("myDocument.txt"); 
}; 

ReadDocument.cpp:ところで

#include "ReadDocument.h" 

ReadDocument::ReadDocument(){} 

void ReadDocument::documentContent(){ 
    if(myflux){ 
     //Getting the words from the file string by string 
     std::string data; 
     while(myflux>>data){ 
      myList.push_back(data); 
     } 

     myflux.close(); 
    } 
    else{ 
     std::string message = "\t\tERROR ==> This file doesn't exist."; 
     throw FileDoesntExistException(message); 
    } 
} 

std::ostream& operator <<(std::ostream& os, const ReadDocument& d){ 
    for(std::list <std::string> :: const_iterator it = ReadDocument::myList.begin(); it!=ReadDocument::myList.end(); it++) 
    std::cout<<*it<<std::endl; 

    return os; 
} 

、あなたは空のコンストラクタを作成し、何でそれを充填することは避けてください。
C++ 11以降では、ヘッダにReadDocument() = default;と書いて、ReadDocument::ReadDocument(){}をcppから削除する必要があります。あなたが variablesとあなたのクラスの各instanceにこれらdefaultcharacteristicsを与えるためにしようとしているあなたのclass

1

。溶液中で次のようにdefaultconstructorにデフォルトの条件を入れて:

class ReadDocument{ 
private: 
    std::list<std::string> myList; //moved member variables to private 
    std::ifstream myflux;   //part of class 
public: 
    ReadDocument(); //empty constructor 
    ReadDocument(const std::string& fileName); //constructor 

    void documentContent(); 

    friend std::ostream& operator <<(std::ostream& os, const ReadDocument& d); 
}; 

実装:

#include "ReadDocument.h" 
ReadDocument::ReadDocument() 
{ 
    myflux.open("myDocument.txt"); 
    if(myflux){ 
      //Getting the words from the file string by string 
      std::string data; 
      while(myflux>>data){ 
       myList.push_back(data); 
      } 

      myflux.close(); 
    } 
    else{ 
     std::string message = "\t\tERROR ==> This file doesn't exist."; 
     throw FileDoesntExistException(message); 
    } 
} 

void ReadDocument::documentContent(){} 

std::ostream& operator <<(std::ostream& os, const ReadDocument& d){ 

    for(std::list <std::string> :: const_iterator it =   
    d.myList.begin(); it!=d.myList.end(); it++) 
     std::cout<<*it<<std::endl; 

    return os; 
} 

でインスタンス化しない限り、コードは現在(デフォルトでmyListfile data"myDocument.txt"myfluxを関連付けるとextractれますconst文字列&コンストラクタで使用する文字列パラメータ)instanceReadDocumentclassのそれぞれに対して。 ostream関数の他のいくつかの修正点もあります。

PS * ReadDocumentclassに関連付けられたファイル全体が.cppであると仮定すると、conststring&parameter,ReadDocument(const std::string& fileName)を含む忘れたconstructorです。

+0

現代のC++では、クラス定義内の非スタティックメンバのイニシャライザを指定することは完全に合理的です(適切な構文を使用する限り、つまりイニシャライザの統一構文)。 – AnT

+0

@AnT私はこれらの行に沿って何かを読んでいましたが、charやintなどの組み込み型にもっと関係していると思います。しかし、ifstreamはistreamオブジェクトです。私は当然間違いがあります。 –

+0

いいえ、任意のタイプの非静的メンバーの初期化子を記述できます。これらのイニシャライザは、基本的に、これらのメンバのコンストラクタイニシャライザリストエントリを暗黙的に生成するために使用されます。 – AnT

関連する問題