Skip to main content
GameDev.net gamedev.net
🔒 Locked

STL Error - C2784

Started by rKallmeyer Mar 6, 2005 at 2:07 PM 1 replies 3.1k views
Original Post
rKallmeyer
rKallmeyer
I'm trying to use the stl map class to hold cells for my game map (irony.) I have a std::map < D3DXVECTOR3, MAPCELL > cells_; with the purpose of allowing my map design to be very flexible. ( All of the rendering code is completely abstracted from this, so dont worry about speed ) Now I try to start inserting map cells. cells_[ D3DXVECTOR3 ( 1.0f, 1.0f, 0.0f ) ] = MAPCELL (...); when I compile I get a long list of errors, all C2784 : "The compiler cannot determine a template argument from the supplied function arguments". I'm pretty new to STL and the vs.net help on the error is a joke. Anyone think they might be able to help me out?
rKallmeyer
rKallmeyer
Figured it out. Just thought I would post the solution in case anyone else ran into the same problem in the future.

// I needed to create my own less comparison like follows:struct vector3_less : public std::binary_function<VECTOR3, VECTOR3, bool>{    bool operator()(const VECTOR3& left, const VECTOR3& right) const    {        return (left.y < right.y && 				left.z < right.z &&				left.x < right.x );    }};// To declare the map with a VECTOR3 as a keystd::map < VECTOR3, MAPCELL, vector3_less >	cells_;

Zahlman
Zahlman
Warning! That will compile, but is likely to break at run-time. The problem is that your comparison operator does not impose a 'strict weak ordering' as it needs to. In particular, you may have two D3DXVECTOR3s such that neither is less than the other, even though they are not equal. This causes problems for the map implementation, because it is basically a binary search tree - without a proper ordering, it may not store things in a consistent manner, which means lookups/deletions can fail for things that are indeed present.

Try this instead:
bool operator()(const VECTOR3& left, const VECTOR3& right) const {  if left.y < right.y return true; if left.y > right.y return false;  // if we get here, the y's are equal  if left.z < right.z return true; if left.z > right.z return false;  // if we get here, the z's are equal  return left.x < right.x; // else, left.x is greater or equal -> left is not less}


Another way to solve the problem would be to wrap the structure, providing an operator<, and use that in the map:

class ComparableVector3: public VECTOR3 {  bool operator<(const VECTOR3& rhs) const {    // same algorithm here...  }}std::map<ComparableVector3, MAPCELL> cells_;

Topic Locked

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

Sign in to reply to this topic.