题
不幸的是,我仍然从这里遇到了模板代码问题:
在文件“实用程序”中的第49行上:
error C2440: 'Initializing': cannot convert from 'const int' to 'IntersectionData *'
error C2439: 'std::pair<_Ty1,_Ty2>::second': member could not be initialized
我怎么能弄清楚问题在哪里?我唯一与“ IntersectionData*”配对的地方在这里:
#include "MRMaterialMatth.h"
#include "IntersectionData.h"
using namespace std;
struct IShaderMatth {
virtual ~IShaderMatth() {}
vector<pair<MaterialMatth,IntersectionData*> > traceCols;
};
而且没有其他编译器错误
我该如何追踪?
//编辑:实用程序不是我的代码。它必须来自STD。第49行的代码看起来像这样:
template<class _Other1,
class _Other2>
pair(const pair<_Other1, _Other2>& _Right)
: first(_Right.first), second(_Right.second)
{ // construct from compatible pair
}
第49行是评论的行
EDIT2:我唯一改变有关Tracecols内容的地方看起来像这样:
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);
}
无效问题吗?这是一个指针,所以我应该允许我用null创建一对,不?
解决方案
尝试明确施放 NULL
至 IntersectionData *
在您的电话中 make_pair()
.
if(traceCols){
traceCols->push_back(make_pair(MaterialMatth(), (IntersectionData *)NULL));
}
其他提示
初始化其中一对存在问题。
问自己:“什么初始化?”
答案是矢量tracecols。
现在问:“我在哪里在Tracecols创建元素?”
一旦回答,您应该知道出了什么问题。
当心 (*traceCols)[traceCols->size()].second = new IntersectionData(*newIData)
- 似乎这将超出向量的界限(因为向量的最大索引是 size() - 1
).
我不确定零是造成的 - 因此请发表评论,然后亲自查看(或尝试Dave的建议)!如果它不起作用,请发表其他评论。最终,您要么找到什么行,并且能够问一个更具体的问题,要么就不是这些问题,并且您知道您必须在其他地方搜索。当我看到所有这些愚蠢的编译器错误消息时,这就是我要做的。
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.