template arguments errors...

Started by
7 comments, last by Seriema 20 years, 11 months ago
Hi! I''m writing a ifstream derived class and making specializations for the >> operators.
  
class blockreader : public ifstream
{
...
template <class T>
blockreader &blockreader::operator >>(T &t)

template <class T>
blockreader &operator >>( TimeKey<T>& key );

template <class T, int C, int R>
blockreader &operator >>(const Matrix<T,C,R>& m);

template <class T>
blockreader &operator >>(const Matrix3x3<T>& m);

template <class T>
blockreader &operator >>(const Matrix4x4<T>& m);

...
};
  
There are Vectors and Color too... My problem is: error C2679: binary ''>>'' : no operator found which takes a right-hand operand of type ''ORC::TimeKey'' (or there is no acceptable conversion) with [ T=ORC::Color ] and another problem is: c:\Program\Microsoft Visual Studio .NET\Vc7\include\xutility(1130): error C2679: binary ''='' : no operator found which takes a right-hand operand of type ''ORC::TimeKey'' (or there is no acceptable conversion) with [ T=ORC::Color ] Note: I get 2 more errors of both, one with T=float and the other T=Vector3f. I just don''t get it... I have a Color::operator=(const Color&) and I have blockreader::operator>>(Color&) Somebody please help me! thanx... "No lies of sugar can sweeten the sournes of reality" }+TITANIUM+{ A.K.A. DXnewbie[onMIRC]
[ ThumbView: Adds thumbnail support for DDS, PCX, TGA and 16 other imagetypes for Windows XP Explorer. ] [ Chocolate peanuts: Brazilian recipe for home made chocolate covered peanuts. Pure coding pleasure. ]
Advertisement
you haven''t shown the line of code that produced the error

do the lines say...

blockreader reader;
ORC::TimeKey value;
value >> reader;// error

?

With your code as it is you''d have to call it like this

reader.operator>>(value);

You need to make it a non member function as is the normal way of doing things. Check out documentation for complex for an example of how they are declared
oki... you asked for it...

the first (>>) error:

      template <class T>blockreader &blockreader::operator >>(T &t){  if (c)    c->Add(&t, sizeof(T));  else  {    std::ifstream &i = *this;    if (binary)      i.read(reinterpret_cast<char *>(&t), sizeof(T));    else    {      eatcomments();      i >> t;  // <--HERE!!!    }  }  return *this;}  


the second (=) error:


  template<class _InIt,	class _OutIt> inline	_OutIt _Copy_opt(_InIt _First, _InIt _Last, _OutIt _Dest,		_Nonscalar_ptr_iterator_tag)	{	// copy [_First, _Last) to [_Dest, ...), arbitrary iterators	for (; _First != _Last; ++_Dest, ++_First)		*_Dest = *_First;  // <--HERE!!!	return (_Dest);	}      




how come it's better to be "non-members"? and then, in what file should I write them? In blockreader.h/.cpp, TimeKey.h/.cpp (for the >>(TimeKey), and respectivly for the others), or a brand new .h/.cpp file?

[edited by - Seriema on May 7, 2003 7:12:15 AM]

[edited by - Seriema on May 7, 2003 7:12:41 AM]
[ ThumbView: Adds thumbnail support for DDS, PCX, TGA and 16 other imagetypes for Windows XP Explorer. ] [ Chocolate peanuts: Brazilian recipe for home made chocolate covered peanuts. Pure coding pleasure. ]
it''s not that it''s better, it''s that you can''t call it the way you''re calling it.

I''ve shown you how you have to call your functions

object.operator>>(otherObject);

You actually need the non member binary operator with a lhs (left hand side) and rhs:

object& operator>>(object& lhs, otherObject& rhs);

This can then be called

object a;
otherObject b;

a >> b;//a binary operator with a lhs (left hand side) and rhs
you need to put them in the header

the actual implementation of the read/write function could be a member

eg


  //.hclass example {    int number;public:    istream& read(istream& is);    ostream& write(ostream& os);};//.cppistream& example::read(istream& is){    is >> number;    return is;}//template headeristream& operator>>(istream& lhs, example& rhs){    return rhs.read(lhs);}  


so you can do

istream is;
ostream os;
example e;
e.read(is);
e.write(os);
//and
is >> e;
os << e;
the first (>>) error:

i >> t;

but i is of type ifstream, not of type blockreader.

Why do you need an alias variable?
If it is to avoid complex pointer syntax then your alias i had to be of blockreader type.

P.S:
substitute the code (i >> t; ) by
this->operator>>(t);

[edited by - Pepe on May 7, 2003 7:50:38 AM]

P.S.S:
fucked icons

[edited by - Pepe on May 7, 2003 7:51:29 AM]
thanx for all the tips, I''ve got Read() and Write() on most things, but I wanted simple "structures" like Vector, Color, etc. to be able to << and >>

the first error is gone!! *yeey* did as Pepe said, thank you!!

but the second one is still there... anyone?..

"No lies of sugar can sweeten the sournes of reality"

}+TITANIUM+{ A.K.A. DXnewbie[onMIRC]
[ ThumbView: Adds thumbnail support for DDS, PCX, TGA and 16 other imagetypes for Windows XP Explorer. ] [ Chocolate peanuts: Brazilian recipe for home made chocolate covered peanuts. Pure coding pleasure. ]
About the second problem I need more code. The lines in what you use the function _Copy_opt.

If you only need << and >> stuff it's not necessary to subclass.

It's more easy a friend function:

For example:

In the Color.h

      class Color {   friend ostream & operator<<(ostream & os, const Color & c);   ...   int r, g, b;};ostream & operator<<(ostream & os, const Color & c);  



And in the Class.cpp


      ostream & operator<<(ostream & os, const Color & c) {   os << c.r << c.g << c.b;   return os;}      ...  


NOTE: The use of friend keyword is only necessary if you need to access the private things of the class, if you only use public functions then you can avoid the friend declaration.

It makes me really happy that my suggestions were useful for you :-)

I always forget source tags

[edited by - Pepe on May 7, 2003 9:53:52 AM]
I removed a std::copy() call:

  template <class T>inline bool KeyFrame<T::Save(blockwriter &file, bool binary = true){copy(keyFrames.begin(), keyFrames.end(), ostream_iterator<char>(file, " "));return true;}  


with a simple loop.
now it works! and it''s more readable too!

but still... why didn''t that work?.. it copies with op=, and the op= for all the types it complained for exists..?

"No lies of sugar can sweeten the sournes of reality"

}+TITANIUM+{ A.K.A. DXnewbie[onMIRC]
[ ThumbView: Adds thumbnail support for DDS, PCX, TGA and 16 other imagetypes for Windows XP Explorer. ] [ Chocolate peanuts: Brazilian recipe for home made chocolate covered peanuts. Pure coding pleasure. ]

This topic is closed to new replies.

Advertisement