Templated operator>>

This is a discussion on Templated operator>> within the C++ Programming forums, part of the General Programming Boards category; When trying to use the following code: Code: template<class T> class StreamSource { T source; public: StreamSource() : source() { ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Location
    Brasil
    Posts
    220

    Templated operator>>

    When trying to use the following code:

    Code:
    template<class T>
    class StreamSource
    {
    	T source;
    public:
    	StreamSource() : source()
    	{
    	}
    
    	const T& getSource()
    	{
    		return source;
    	}
    
    	template<class Y>
    	VideoStream<T, Y> operator>>(Y* nextObject)
    	{
    		return VideoStream<T, Y>(this, nextObject);
    	}
    };
    Gives me the following compiler output:
    Code:
    VideoStream.h(44) : error C2665: 'VideoStream<T,Y>::VideoStream' : none of the 3 
    overloads could convert all the argument types
    1>        with
    1>        [
    1>            T=Source,
    1>            Y=Etc
    1>        ]
    k:\felipe\projects\scarvideo\scarvideo\VideoStream.h(14): could be 'VideoStream<T,Y>::VideoStream(T *,Y *)'
    1>        with
    1>        [
    1>            T=Source,
    1>            Y=Etc
    1>        ]
    while trying to match the argument list '(StreamSource<T> *const , Etc *)'
    1>        with
    1>        [
    1>            T=Source
    1>        ]
    .\main.cpp(47) : see reference to function template instantiation 'VideoStream<T,Y>
     StreamSource<T>::operator >><Etc>(Y *)' being compiled
    1>        with
    1>        [
    1>            T=Source,
    1>            Y=Etc
    1>        ]
    All i wanted to do is to create the object VideoStream with the first type the same of the VideoSource::source type and the second type of the type provided by the operator>>. What&#180;s wrong?

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    2,053
    Code:
    VideoStream<T, Y> operator>>(Y* nextObject)
    	{
    		return VideoStream<T, Y>(this, nextObject);
    	}
    These two types need to agree.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  3. #3
    Registered User
    Join Date
    Oct 2005
    Location
    Brasil
    Posts
    220
    Code:
    template<class T, class Y>
    class VideoStream
    {
    	T* left;
    	Y* right;
    public:
    	/*VideoStream() : left(), right() 
    	{
    	}*/
    
    	VideoStream(T left, Y right)
    	{
    		this->left  = left;
    		this->right = right;
    	}
    
    	void nextFrame(unsigned char** buffer)
    	{
    		left->nextFrame(buffer);
    		right->nextFrame(buffer);
    	}
    };
    
    template<class T>
    class StreamSource
    {
    	T source;
    public:
    	StreamSource() : source()
    	{
    	}
    
    	const T& getSource()
    	{
    		return source;
    	}
    
    	template<class Y>
    	VideoStream<T*, Y*> operator>>(Y* nextObject)
    	{
    		return VideoStream<T*, Y*>(this, nextObject);
    	}
    };
    Even matching types, i still get errors, now i have posted the VideoStream class too. Ideas?

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    in the VideoStream constructor you are attempting to initialize 'left' and 'right' with non-pointers. correct that and use the orignal code for StreamSource and it should work. BTW, the use of that operator overload seems a bit convoluted...
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    2,053
    You have the same problem in VideoStream:
    Code:
    VideoStream(T left, Y right)
    {
    	this->left  = left;
    	this->right = right;
    }
    this->left and this->right are of types T* and Y*. left and right are of types T and Y. These do not agree.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. templated members in non-template classes
    By drrngrvy in forum C++ Programming
    Replies: 3
    Last Post: 04-07-2007, 02:38 PM
  2. Templated classes...
    By Wraithan in forum C++ Programming
    Replies: 7
    Last Post: 09-14-2006, 05:31 PM
  3. Passing a templated function for WNDPROC
    By Smallz in forum Windows Programming
    Replies: 1
    Last Post: 08-29-2006, 07:13 AM
  4. Templated member functions
    By Highland Laddie in forum C++ Programming
    Replies: 5
    Last Post: 05-15-2006, 06:25 PM
  5. Replies: 3
    Last Post: 10-10-2002, 08:34 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21