473,395 Members | 1,869 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

custom istream class

Hi,

I'm trying to derive my own stream class from std::basic_istream and
I'm having problems getting operator>> to work. Consider the following
little program:
#include <iostream>
#include <istream>
#include <bits/char_traits.h>

template<typename _CharT, typename _Traits>
class myistream : public std::basic_istream<_CharT, _Traits>
{
// constructor
myistream() : std::basic_istream<_CharT, _Traits>(NULL) { }

// destructor
~myistream() { }
};

int main(int argc, char** argv)
{
myistream<char, std::char_traits<char> > is();

char c;
is >> c;
std::cout << c << std::endl;
}
Compiling it I get the following error:

test.cpp: In function 'int main(int, char**)':
test.cpp:22: error: invalid operands of types 'myistream<char,
std::char_traits<char> > ()()' and 'char' to binary
'operator>>'

I've looked at the fstream header to see how basic_ifstream is derived
from basic_istream and to me it looks like my little test program does
essentially the same. Any Ideas?

Thanks in advance,
Martin.

Aug 12 '05 #1
1 2711

<ma****@gmx.de> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
Hi,

I'm trying to derive my own stream class from std::basic_istream and
I'm having problems getting operator>> to work. Consider the following
little program:
#include <iostream>
#include <istream>
#include <bits/char_traits.h>

template<typename _CharT, typename _Traits>
class myistream : public std::basic_istream<_CharT, _Traits>
{
// constructor
myistream() : std::basic_istream<_CharT, _Traits>(NULL) { }

// destructor
~myistream() { }
};

int main(int argc, char** argv)
{
myistream<char, std::char_traits<char> > is();
The compiler thinks your are declaring a function named 'is' that returns a
myistream<char, std::char_traits<char> > and takes no arguments, get rid of
the () and it should compile fine.

char c;
is >> c;
std::cout << c << std::endl;
}
Compiling it I get the following error:

test.cpp: In function 'int main(int, char**)':
test.cpp:22: error: invalid operands of types 'myistream<char,
std::char_traits<char> > ()()' and 'char' to binary
'operator>>'

I've looked at the fstream header to see how basic_ifstream is derived
from basic_istream and to me it looks like my little test program does
essentially the same. Any Ideas?

Thanks in advance,
Martin.

Aug 12 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Thomas Matthews | last post by:
Hi, In some threads, some people mentioned that variable initialization is best performed in an initialization list. Is there a way to initialize a variable from an istream in an...
3
by: matthurne | last post by:
I'm doing a chapter 12 exercise from Accelerated C++ ... writing a string-like class which stores its data in a low-level way. My class, called Str, uses a char array and length variable. I've...
13
by: Randy | last post by:
Is there any way to do this? I've tried tellg() followed by seekg(), inserting the stream buffer to an ostringstream (ala os << is.rdbuf()), read(), and having no luck. The problem is, all of...
2
by: Raf256 | last post by:
Hello, my custom streambuf works fine with output via << and with input via .get() but fails to input via >> or getline... any idea why? -------------------- A custom stream buffer (for...
0
by: ChopStickr | last post by:
I have a custom control that is embedded (using the object tag) in an html document. The control takes a path to a local client ini file. Reads the file. Executes the program specified in...
2
by: Colonel | last post by:
It seems that the problems have something to do with the overloading of istream operator ">>", but I just can't find the exact problem. // the declaration friend std::istream &...
3
by: Kourosh | last post by:
Hi all, I'm trying to call a COM function from C# The function gets a paramter of type IStream in C++. I've found the type System.Runtime.InteropServices.ComTypes.IStream, however I'm not sure...
0
by: admb600 | last post by:
I wouldn't normally beg but my dissertation is due in, in a couple of weeks and I am in way over my head here.. Fixing this issue will make or break the project and my degree.. The goal is to...
4
by: james.lawton | last post by:
Hi, I'm having a problem that I can't diagnose. I'm creating istreams of unknown types, and want to manage them on the stack, co I'm passing around ownership-holding pointers. Usually, I would...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.