Solved

overloading the getline function of iostream

Posted on 1997-04-20
1
652 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C++ dll will not run on machine without VS installed 10 80
Create a path if not exists 7 69
Safe conversion? 4 58
max float value 3 26
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
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 user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

896 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

11 Experts available now in Live!

Get 1:1 Help Now