期望头文件中有一个参数

expected a parameter in the header

本文关键字:有一个 参数 文件 期望      更新时间:2023-10-16

我试图读取和显示一个名为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 = "ttERROR ==> 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;
}

我试着重命名文件,仍然是同样的问题。我把它放在课堂上,没有改变任何东西。我总是得到同样的错误。是的,这是我的整个代码

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 = "ttERROR ==> 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;,并从cpp中删除ReadDocument::ReadDocument(){}

与你的class你是declaring两个public variables,并试图给每个instance你的类这些default characteristics。在default constructor中设置默认条件的解决方案如下:

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 = "ttERROR ==> 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;
}

代码现在默认将myflux"myDocument.txt"extract关联,file datamyList关联(除非使用string参数实例化以与const string&ReadDocumentclass的每个instance的构造函数)。以及ostream函数中的其他一些修复。

p。S*假设这是与ReadDocument class相关联的整个.cpp文件,您忘记实现包含const string& parameter, ReadDocument(const std::string& fileName)constructor .

相关文章: