c++ - 期望标题中的参数

标签 c++

我正在尝试读取和显示名为 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;
}

我试过重命名文件,但仍然是同样的问题。 我把它放在类里面,并没有改变任何东西。 我总是有同样的错误出来。 是的,这是我的全部代码

最佳答案

myflux 之后的类部分不在方法中。
您是否想像这样将代码放在函数 documentContent 中?


读取文档.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");
};

读取文档.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 开始,您应该在 header 中编写 ReadDocument() = default; 并从 cpp 中删除 ReadDocument::ReadDocument(){}

关于c++ - 期望标题中的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40334246/

相关文章:

c++ - 模板特化可以进入我的 .cpp 吗?

c++ - Netbeans C++ MinGW 设置

c++ - 在 C/C++ 中获取文件大小的可移植方法

java - Java有堆和栈吗?

c++ - 如何优化画圆?

c++ - 让 Loki Singleton 在 VS 2008 C++ 的 DLL 中工作

c++ - 用 c++ 编写的程序可以在任何地方运行吗?

c++ - Windows 下 HUGE_VALF 未定义

c++ - 共享指针生命周期

c++ - 如何在 Gui win32 编程中使用静态标签和按钮