Solved

overloading the getline function of iostream

Posted on 1997-04-20
1
654 Views
Last Modified: 2008-03-06
I have been trying to overload the getline function of
iostream to work with my String class shown below. The
code is supposed to just open a file (WORDS) and count
the number of lines. The error I'm getting is as follows:

class istream &istream::getline(char *, int, char)
cannot convert parameter1 from class String to char *.

Any suggestions please? Thankyou very much.


#ifndef STRING_H



#include <iostream.h>
#include <stdlib.h>
#include <fstream.h>

// Declaration section

const int MAX_STRING_LENGTH = 21;

class String
{
public:

   // Class constructor

// String();
// String(const char str[MAX_STRING_LENGTH + 1]);
// String(const String &str);

   // Function members

   friend istream& getline(istream &is, String &str);
   
private:

   // Data members

   char data[MAX_STRING_LENGTH + 1];

};

#define STRING_H

#endif



int main()
{
int count = 1;
String word;

ifstream infile;

   infile.open("WORDS");
   if (infile.fail())
   {
      cerr << "Input file opening has failed.\n";
   }  exit (1);

infile.getline(word, MAX_STRING_LENGTH + 1);

while (!infile.eof())
   {
      count++;
      infile.getline(word, MAX_STRING_LENGTH + 1);
   }

cout << "There are " << count << " lines of data\n";

infile.close();

return 0;

}

istream& getline(istream &is, String &str)
{
   is.getline(str.data, MAX_STRING_LENGTH + 1);
   return is;
}
0
Comment
Question by:dch500
1 Comment
 

Accepted Solution

by:
Sigma031497 earned 50 total points
ID: 1162869
getline is a method belonging to istream, and not a function declared outside it. I suspect you got confused with operator >>, which is defined as a separete function outside of istream, and which you would overload the way you did above. To overload getline you have to create a descendant class of istream like this:

class my_istream : public istream
{
  public:
...
  istream &getline(String &str) {
    ...
    return *this
  }
...
};


This is not particularly pretty, though, so I suggest you use the operator >> like this:

istream &operator >> (istream &s, String &str)
{
  ...
  return s;
}

Then you will be able to say

main ()
{
  ...
  String &mystring;
  cin >> mystring;
  ...
}



Sigma
0

Featured Post

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question