Solved

overloading the getline function of iostream

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to convert MFC::CString to UTF8 wchar_t* 10 387
Arduino EDI - Programming language 3 117
Issues with C++ Class 19 108
What is sub-make ? 2 88
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

726 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