Solved

overloading the getline function of iostream

Posted on 1997-04-20
1
648 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
Comment Utility
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

772 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now