• Advertisement
Sign in to follow this  

std::valarray and classes

This topic is 4323 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, Has anyone used valarrays with classes and overloaded operators before in vs2005? I'm attempting to write a nicer version of some vertex blending code I wrote long ago using a valarray of vertex classes. I take one slice of the previous frame's vertices, another of the next frames and add them together with weighting.
//Setup a slice to extract previous frame's vertices from vertex valarray
slice PreviousFrameSlice(NumVerticesPerFrame*StartFrame,1,NumVerticesPerFrame);		
//Setup a slice to extract next frame's vertices from vertex valarray
slice NextFrameSlice(NumVerticesPerFrame*(StartFrame+1),1,NumVerticesPerFrame);	
//Calculate vertex blending amount

The class has the operators overloaded that I would have thought it needed:
//Vertex structure for use in models with only 1 set of texture coords
class ModelVertex
		float x;
		float y;
		float z;
		float TextureCoords[2];
		float Normals[3];

		//Overload * for scalar multiplication
		ModelVertex operator*(float K)
			ModelVertex NV;


			return NV;
		//Overload + for addition
		ModelVertex operator*(ModelVertex V)
			ModelVertex NV;


			return NV;	

But the code just won't compile, vc2005 comes up with:
.\src\CModel.cpp(128) : error C2678: binary '*' : no operator found which takes a left-hand operand of type 'std::slice_array<_Ty>' (or there is no acceptable conversion) with [ _Ty=ModelVertex ]
Has anyone ever used valarrays with classes? It would seem to go against the whole stl/c++ philosophy if what you could do with base types in a valarray can't be done with objects.. Cheers Jamie

Share this post

Link to post
Share on other sites
Vertices[PreviousFrameSlice] gives a slice_array, whose members don't include an operator*. But valarray does have operator* so you could try something like
InBetweenedVertices = valarray<ModelVertex>(this->Vertices[PreviousFrameSlice]) * (1.0f-Amount) + valarray<ModelVertex>(this->Vertices[NextFrameSlice]) * Amount;
but the problem is that it only has operator* where both sides are of similar type (i.e. where one side is T and the other is valarray<T>, or both are valarray<T>) - so I think you'd have to declare something new like
template <typename T, typename S> inline valarray<T> operator*(const valarray<T>& a, const S& b) {
valarray<T> r(a.size());
for (size_t i = 0; i < r.size(); ++i)
r = a * b;
return r;
to make it work.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement