?
Solved

overloading the getline function of iostream

Posted on 1997-04-20
1
Medium Priority
?
687 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 

Accepted Solution

by:
Sigma031497 earned 150 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses
Course of the Month14 days, 15 hours left to enroll

771 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