I've made a class that inherits std::vector, to make it more like C#'s List.
But the subscript operator is given me trouble, that I can't figure out.
Especially as the code worked fine in my test project.
From List.h
template<class T> class List : protected std::vector<T>
{
/* unimportant stuff */
T &operator[]( const int i )
{
assert( i >= 0 && i < Count() );
return vector::operator[]( i );
}
};
And this where I'm getting the error:
error C2678: binary '[' : no operator found which takes a left-hand operand of type 'const List<Matrix4>' (or there is no acceptable conversion)
float *RenderObject::TransformData( int index ) const
{
assert( index < transforms.Count() );
return (float*)&transforms[index];
}
I'm not getting this in my test project. How come?
You get an error because you have not mentioned the template argument of vector.
return vector<T>::operator[]( i );
Template code is usually not instantiated until you use it. Your test program probably didn't use the List<T>::operator[] so the code in that function was never fully evaluated.
I've unfortunately already tried this, and still been getting the same error. The test program does actually use the [] operator. I delibrately changed a section of the code there to do so, but still not having that issue.
From the test program
void Display( List<Item> list )
{
cout
<< "# Contents" << endl
<< "----------" << endl;
for( int i = 0; i < list.Count(); i++ )
{
cout << i << " " << list[ i ].Name() << endl;
}
cout << endl;
}