-
March 22nd, 2009, 07:44 PM
#1
Template stream operator overloading
I want to be able to put the template class below in a cin and cout and thereby be able to input/output the value contained within. Has anyone done this before and know how it could be done?
Code:
template<class T> SomeClass
{
public:
SomeClass(T inval) { value = inval; }
protected:
T value;
}
Example of what I want to be able to do:
Code:
SomeClass someObject<int>(5);
cout << someObject << endl;
When run, I want this code to output '5'. Thanks
Intel Core Duo Macbook w/ Mac OS 10.5.6
gcc 4.2.1 (i386-apple-darwin9.1.0) and Xcode 3.1.1
-
March 22nd, 2009, 08:37 PM
#2
Re: Template stream operator overloading
Originally Posted by Etherous
Has anyone done this before and know how it could be done?
Quite trivial to implement:
Code:
template<class T> class SomeClass
{
public:
SomeClass(T inval) : value(inval) {} //This is real initialization.
friend std::ostream& operator<<(std::ostream& outputStream, const SomeClass& instance)
{
return outputStream << instance.value;
}
protected:
T value;
};
EDIT: Forgot about the second part of your question; it's the same idea with operator>>, but you'll be dealing with std::istream (in the case of std::cin).
Last edited by Plasmator; March 22nd, 2009 at 08:48 PM.
-
March 22nd, 2009, 11:02 PM
#3
Re: Template stream operator overloading
Well, not surprisingly, it works. I feel stupid because it's such a simple thing to do. I just haven't needed to do this for quite some time. Thanks.
Last edited by Etherous; March 22nd, 2009 at 11:10 PM.
Intel Core Duo Macbook w/ Mac OS 10.5.6
gcc 4.2.1 (i386-apple-darwin9.1.0) and Xcode 3.1.1
-
March 23rd, 2009, 03:09 AM
#4
Re: Template stream operator overloading
Is there any approach that works with any class including different variable ?
This is tedious because you need to write this every time for every class.
Thanks for your help.
-
March 23rd, 2009, 03:36 AM
#5
Re: Template stream operator overloading
Originally Posted by Peter_APIIT
Is there any approach that works with any class including different variable ?
This is tedious because you need to write this every time for every class.
The compiler can't read your mind (yet).
-
March 23rd, 2009, 08:29 PM
#6
Re: Template stream operator overloading
I mean does generic programming able to handle such a tedious task for every class.
Thanks for your help.
-
November 1st, 2009, 06:07 PM
#7
Re: Template stream operator overloading
I have a template class called basic_posint, in which I have declared a friend function
Code:
template<unsigned BASE>
class base_posint : public integer {
...
public:
friend std::ostream& operator <<(std::ostream&, const basic_posint);
}
but the compiler gives me the following error:
Code:
integer.hpp:85: error: ISO C++ forbids declaration of `basic_posint' with no type
The function is declared as public. What does it mean, doesn't it recognize basic_posint as a typename?
Edit: I also tried your example template class, and it compiled without any warnings. On the other hand, if I replaced the function body by a semi colon (hence defining the function somewhere else), I got the following warning:
Code:
integer.hpp:48: warning: friend declaration `std::ostream& operator<<(std::ostream&, const SomeClass<T>&)' declares a non-template function
integer.hpp:48: warning: (if this is not what you intended, make sure the function template has already been declared and add <> after the function name here) -Wno-non-template-friend disables this warning
Is it supposed to be like this? Do you have to define the function within the class to prevent this warning? (I'm compiling with g++)
Last edited by TriKri; November 1st, 2009 at 06:20 PM.
-
November 2nd, 2009, 12:11 AM
#8
Re: Template stream operator overloading
Originally Posted by TriKri
I have a template class called basic_posint
Your class template is named base_posint. You need to decide if you want to use base_posint or basic_posint.
Originally Posted by TriKri
Do you have to define the function within the class to prevent this warning? (I'm compiling with g++)
No. Read this FAQ on Why do I get linker errors when I use template friends? for a solution.
-
November 2nd, 2009, 02:24 AM
#9
Re: Template stream operator overloading
Originally Posted by laserlight
Doh! XD Sometimes I feel really stupid. I replaced basic_posint with base_posint, and the problem got solved. Strange, huh? :P
Concerning the warnings I got from the compiler, I had to do both of the things that were mentioned in your link in order to get it to compile. If I remove the <> template indication I get a warning and then undefined reference to `operator<<(std::ostream&, base_posint<10u> const&)' and it won't compile, and if I remove the pre-declaration I get an error saying my friend function does not match any template declaration. But now it works just fine. Thanks!
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|