views:

219

answers:

4

Hi!

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

http://stackoverflow.com/questions/1911434/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));

and

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

and

  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??

A: 

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.

Zan Lynx
could be the problem that i create a pair with NULL? NULL probably is a const int..
Mat
A: 

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.

Tim Sylvester
+2  A: 

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

if(traceCols){
        traceCols->push_back(make_pair(MaterialMatth(), (IntersectionData *)NULL));
}
Dave Hinton
hey yeah - that solved the Problem! but i'd be really glad if someone could explain me why this is necessary?
Mat
In Visual Studio (which I assume you're using from the error messages), `NULL` is defined as a literal `0`, so your use of `make_pair` becomes `make_pair(MaterialMatth(),0)`, of type `pair<MaterialMatth,int>`.
Mike Seymour
In fact, the C++ standard demands that `NULL` be defined as `0`, regardless of which compiler you're using.
Shmoopty
+1  A: 

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.

Smashery