Skip to main content
gamedev.net
🔒 Locked

template arguments errors...

Started by Seriema 7 replies 850+ views
Original Post
Seriema
Seriema
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 c
Seriema
Seriema
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 c
Qw3r7yU10p!
Qw3r7yU10p!
you need to put them in the header

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

eg


  //.h

class example {
int number;
public:
istream& read(istream& is);
ostream& write(ostream& os);
};

//.cpp

istream& example::read(istream& is)
{
is >> number;
return is;
}

//template header

istream& 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;
Pepe
Pepe
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]
Seriema
Seriema
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 c
Pepe
Pepe
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]
Seriema
Seriema
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 c

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.