Amine AIT BOUZIAD
Amine AIT BOUZIAD

Reputation: 3

expected a parameter in the header

I am trying to read and display the contents of a file named myDocument.txt, but I always get the following error with my header file.

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

What's going on?

Here is my 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);
    }

};

Here is my 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;
}

And this is my UseReadDocument.cpp

#include "ReadDocument.h"

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

    return 0;
}

I tried renamed the file and still the same problem. I put it in the class, and doesn't change anything. I always have the same error coming out. and yes it is my entire code

Upvotes: 0

Views: 746

Answers (2)

Nick Pavini
Nick Pavini

Reputation: 342

With your class your are declaring two public variables and trying to give each instance of your class these default characteristics. The solution in to put the default conditions in the default constructor as follows:

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);
}; 

Implementation:

#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;
}

Code will now associate myflux with "myDocument.txt" and extract the file data into myList by default (unless instantiated with string parameter for use with const string& constructor) for each instance of the ReadDocument class. As well as a few other fixes in the ostream function.

P.S* Assuming this is your entire .cpp file associated with the ReadDocument class, you forgot to implement the constructor containing the const string& parameter, ReadDocument(const std::string& fileName).

Upvotes: 1

Jan Hohenheim
Jan Hohenheim

Reputation: 3752

The part of your class after myflux is not in a method.
Did you want to put the code in the function documentContent, like this?


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;
}

By the way, you should avoid creating an empty constructor and filling it with nothing.
Since C++11 you should write ReadDocument() = default; in your header and remove ReadDocument::ReadDocument(){} from the cpp.

Upvotes: 1

Related Questions