Sign in to follow this  
de_matt

Strange vector problem

Recommended Posts

Hya, I'm having some really strange problems with vectors in VC++Express. Has something changed since VC++6.0? Maybe I've got something else wrong somewhere but when I try to copy a D3DXVECTOR3 in to a vector<D3DXVECTOR3> I get this message: c:\documents and settings\matts login\my documents\programming\c++ work\terrain\cboundingbox.cpp(27) : error C2679: binary '=' : no operator found which takes a right-hand operand of type 'const D3DXVECTOR3' (or there is no acceptable conversion) The class structure
class CBoundingBox
{
public:
	
	CBoundingBox( const D3DXVECTOR3& v_1 = D3DXVECTOR3(),
				  const D3DXVECTOR3& v_2 = D3DXVECTOR3() )
	{
		blnFirst = true;
		AddPoint( v_1 );
		AddPoint( v_2 );
	}


	// gets
	D3DXVECTOR3					GetV1() const { return v1; }
	D3DXVECTOR3					GetV2() const { return v2; }
	D3DXVECTOR3					GetCentre() const { return ( v1 + v2 ) / 2; }
	std::vector<D3DXVECTOR3>	GetCorners() const;

	// functions
	void AddPoint( const D3DXVECTOR3& v );
	void Clear();


	friend std::ostream& BOUNDINGBOX_Show( std::ostream& out,
										   const CBoundingBox& b );

private:
	
	D3DXVECTOR3 v1, v2;
	bool blnFirst;
};


The function
vector<D3DXVECTOR3>	CBoundingBox::GetCorners()const
{
	vector<D3DXVECTOR3> v[8];
	v[0] = v1;
	v[1] = D3DXVECTOR3( v2.x, v1.y, v1.z );
	v[2] = D3DXVECTOR3( v1.x, v2.y, v1.z );
	v[3] = D3DXVECTOR3( v2.x, v2.y, v1.z );

	v[4] = D3DXVECTOR3( v1.x, v1.y, v2.z );
	v[5] = D3DXVECTOR3( v2.x, v1.y, v2.z );
	v[6] = D3DXVECTOR3( v1.x, v2.y, v2.z );
	v[7] = v2;

	return v;
}


At every point in the function where I try to set an element of 'v' I get the error message. I'm having similar problems with the '<' and '>' signs in this code: Functin decleration
int D3DMaths_SphereIntersectVolume( const std::vector<D3DXPLANE>& planes, 
									const D3DXVECTOR3& centre, 
									float radious );


Function
// does a sphere intersect a volume
int D3DMaths_SphereIntersectVolume( const vector<D3DXPLANE>& planes, 
									const D3DXVECTOR3& centre, 
									float radious )
{
	bool intersect = false;
	bool CentreInside = true;
	vector<float> t[ planes.size() ];

	for( vector<float>::size_type i = 0; i != planes.size(); ++i ) {
		
		//get the dot product of the point with this plane
		t[i] = D3DXPlaneDotCoord( &planes[i], &centre);
		
		// if the point lies beyond the current plane
		if( t[i] > radious ) return D3DMaths_OUTSIDE;
		
		// if the sphere intersects this plane
		if( fabs( t[i] ) < radious ) intersect = true;
		
		if( t[i] > 0 ) CentreInside = false;
	}

	if( intersect ) return D3DMaths_INTERSECT;

	return D3DMaths_INSIDE;
}


The error message for that is: c:\documents and settings\matts login\my documents\programming\c++ work\terrain\d3dmaths.cpp(102) : error C2784: 'bool std::operator >(const std::vector<_Ty,_Alloc> &,const std::vector<_Ty,_Alloc> &)' : could not deduce template argument for 'const std::vector<_Ty,_Alloc> &' from 'int' c:\program files\microsoft visual studio 8\vc\include\vector(1285) : see declaration of 'std::operator >' Any help would be greatly appreciated Thanks Matt

Share this post


Link to post
Share on other sites
inside CBoundingBox::GetCorners() method, I think you would want

vector<D3DXVECTOR3> v(8); // as passing 8 for the constructor instead of [] declaring it as an array

Share this post


Link to post
Share on other sites
Yes, things have definitely changed from VC6, with the current compiler being much more standards compliant.

I'm just guessing here, but doesn't

vector<D3DXVECTOR3> v[8];

give you an array of 8 vector<D3DXVECTOR3>'s, rather than one vector<D3DXVECTOR3> with 8 elements in it? Do you want v(8) instead?

I'm not quite sure what's up with the operator= issue, but I'd suggest experimenting with non-vector assignment from the const data type and try to narrow down the problem some.

Share this post


Link to post
Share on other sites
Quote:

c:\documents and settings\matts login\my documents\programming\c++ work\terrain\cboundingbox.cpp(27) : error C2679: binary '=' : no operator found which takes a right-hand operand of type 'const D3DXVECTOR3' (or there is no acceptable conversion)


As previously mentioned, v/ in your function is an array of vectors, not a vector with eight elements. The expression: v[n] produces a result that is of type std::vector<D3DXVECTOR3>. An expression of the form std::vector<T> = T is meaningless and disallowed, thus the error (to spell it out, the line v[0] = v1; and those that follow attempt to assign a D3DXVECTOR3 to a std::vector<D3DXVECTOR>, which is of course impossible).

Quote:

c:\documents and settings\matts login\my documents\programming\c++ work\terrain\d3dmaths.cpp(102) : error C2784: 'bool std::operator >(const std::vector<_Ty,_Alloc> &,const std::vector<_Ty,_Alloc> &)' : could not deduce template argument for 'const std::vector<_Ty,_Alloc> &' from 'int'
c:\program files\microsoft visual studio 8\vc\include\vector(1285) : see declaration of 'std::operator >'

Same problem. The declaration syntax T t[n] creates an array of T, even if T is a vector. In this case you're attemping to compare a vector and an integer, a nonsensical operation.

To create a std::vector instance with a pre-determined size, you want to use the parameterized constructor supplied by std::vector, as in:

std::vector< T > t(n);

Share this post


Link to post
Share on other sites
Wow I can't believe I did that. I've haven't been doing any programming for a while.

Thanks for spotting it so quickly.

Matt

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this