Using fstream getline () function inside a class

I am trying to load the lines of a text file containing dictionary words into an array object. I want an array to hold all words starting with "a", another one for "b" ... for all letters in the alphabet.

Here's the class I wrote for the array object.

    #include <iostream>
    #include <string>
    #include <fstream>

    using namespace std;

    class ArrayObj
    {
    private:

        string *list;
        int size; 

    public:


        ~ArrayObj(){ delete list;}

        void loadArray(string fileName, string letter)
        {
            ifstream myFile;
            string str = "";
            myFile.open(fileName);

            size = 0;

            while(!myFile.eof())
            {
                myFile.getline(str, 100);

                if (str.at(0) == letter.at(0))
                    size++;
            }
            size -= 1; 

            list = new string[size];

            int i = 0;
            while(!myFile.eof())
            {
                myFile.getline(str, 100);

                if(str.at(0) == letter.at(0))
                {
                    list[i] = str;
                    i++;
                }
            }

            myFile.close();
        }


    };

      

I am getting the error:

2   IntelliSense: no instance of overloaded function     "std::basic_ifstream<_Elem, _Traits>::getline [with _Elem=char, _Traits=std::char_traits<char>]" matches the argument list d:\champlain\spring 2012\algorithms and data structures\weeks 8-10\map2\arrayobj.h  39

      

I'm guessing this requires me to overload the getline function, but I'm not entirely sure how to do this or why this is necessary.

Any advice?

+3


source to share


1 answer


the function for streams that deals with std :: string is not an istream member function, but a free function that is used like that. (the member function version deals with char *).

std::string str;
std::ifstream file("file.dat");
std::getline(file, str);

      

It's worth noting that there are safer ways to do what you are trying to do like this:

#include <fstream>
#include <string>
#include <vector>

//typedeffing is optional, I would give it a better name
//like vector_str or something more descriptive than ArrayObj
typedef std::vector<std::string> > ArrayObj

ArrayObj load_array(const std::string file_name, char letter)
{
    std::ifstream file(file_name);
    ArrayObj lines;
    std::string str;

    while(std::getline(file, str)){
        if(str.at(0)==letter){
            lines.push_back(str);
        }
    }
    return lines;
}


int main(){
    //loads lines from a file
    ArrayObj awords=load_array("file.dat", 'a');
    ArrayObj bwords=load_array("file.dat", 'b');
    //ao.at(0); //access elements
}

      



don't reinvent the wheel; control vectors are standard and will save you a lot of time and pain.

A final attempt not to insert using namespace std

, which is bad for a whole host of reasons I won't go in; instead, prefix std objects with std :: so like std :: cout or std :: string.

http://en.cppreference.com/w/cpp/container/vector http://en.cppreference.com/w/cpp/string/basic_string/getline http://en.cppreference.com/w/cpp/ string

+5


source







All Articles