Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using the fstream getline() function inside a class

I'm trying to load lines of a text file containing dictionary words into an array object. I want an array to hold all the words that start with "a", another one for "b" ... for all the 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'm getting an error saying:

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 guess it's requiring me to overload the getline function, but I'm not quite certain how to go about or why it's necessary.

Any advice?

like image 749
rocklandcitizen Avatar asked Sep 05 '26 11:09

rocklandcitizen


1 Answers

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

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

It is worth noting there are better safer ways to do what you are trying to do like so:

#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; checkout vectors they are standard and will save you a lot of time and pain.

Final try not to put in using namespace std that is bad for a whole host of reasons I wont go into; 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

like image 116
111111 Avatar answered Sep 08 '26 03:09

111111



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!