Tutorial :C++ problem with std::pair and forward declarations


Unfortunately I still got a problem with my templated code from here:

C++ fancy template code problem

on line 49 in the file 'utility':

error C2440: 'Initializing': cannot convert from 'const int' to 'IntersectionData *'    error C2439: 'std::pair<_Ty1,_Ty2>::second': member could not be initialized  

how could i figure out where the problem is? the only place i use a pair with 'IntersectionData*' is here:

#include "MRMaterialMatth.h"  #include "IntersectionData.h"  using namespace std;    struct IShaderMatth {   virtual ~IShaderMatth() {}   vector<pair<MaterialMatth,IntersectionData*> > traceCols;   };  

and there are not any other compiler errors

how can I track down this?

//edit: utility is not my code. it must be from std.. the code of line 49 looks like this:

template<class _Other1,      class _Other2>      pair(const pair<_Other1, _Other2>& _Right)      : first(_Right.first), second(_Right.second)      {   // construct from compatible pair      }  

line 49 is the line of the comment

edit2: the only places where i change something about the content of tracecols look like this:

            IntersectionData* iDataOut = NULL;              if(newIData_out!=NULL)              {                  iDataOut = new IntersectionData(*iData);              }              traceCols->push_back(make_pair(MaterialMatth(),iDataOut));  


    if(traceCols){          traceCols->push_back(make_pair(MaterialMatth(), NULL));      }  


        if(traceCols)          {              (*traceCols)[traceCols->size()].second = new IntersectionData(*newIData);          }  

is NULL the problem? it's a pointer, so i should be allowed to create a pair with NULL, no??


Try explicitly casting the NULL to IntersectionData * in your call to make_pair().

if(traceCols){          traceCols->push_back(make_pair(MaterialMatth(), (IntersectionData *)NULL));  }  


There is a problem initializing one of those pairs.

Ask yourself, "What initializes that?"

The answer is the vector traceCols.

Now ask, "Where am I creating elements in traceCols?"

Once you answer that, you should know what is going wrong.


Watch out for the line (*traceCols)[traceCols->size()].second = new IntersectionData(*newIData) - it seems like that would go out of the vector's bounds (since the largest index of a vector is size() - 1).

I'm not sure if the NULL is causing it - so comment out that line, and see for yourself (or try Dave's suggestion)! If it doesn't work, comment out another. Eventually, you'll either find what line, and be able to ask a more specific question, or it'll be none of those things, and you'll know you have to search somewhere else. That's what I do when I see all these silly compiler error messages.


It looks like you have an assignment somewhere from a pair<MaterialMatth,int>. The compiler is trying to convert from that to the declaration you listed, but it can't convert from an int to a pointer without an explicit cast.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »