C++ std::pair 和前向声明的问题

发布于 2024-08-14 20:51:42 字数 1639 浏览 3 评论 0原文

不幸的是,我的模板代码仍然存在问题:

C++ 花式模板代码问题

在文件“utility”的第 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 的对,不是吗?

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));

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

淡淡的优雅 2024-08-21 20:51:42

尝试在调用 make_pair() 时将 NULL 显式转换为 IntersectionData *

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

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

if(traceCols){
        traceCols->push_back(make_pair(MaterialMatth(), (IntersectionData *)NULL));
}
赤濁 2024-08-21 20:51:42

初始化其中一对时出现问题。

问问自己,“什么初始化它?”

答案是向量traceCols。

现在问,“我在traceCols 中的哪里创建元素?”

一旦你回答了这个问题,你就应该知道出了什么问题。

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.

猫弦 2024-08-21 20:51:42

注意行 (*traceCols)[traceCols->size()].second = new IntersectionData(*newIData) - 看起来这会超出向量的范围(因为最大的向量的索引是 size() - 1)。

我不确定是否是 NULL 导致的 - 所以请注释掉该行,然后亲自查看(或尝试戴夫的建议)!如果不起作用,请注释掉另一个。最终,您要么找到哪一行,并能够提出更具体的问题,要么这些都不是,您就会知道必须在其他地方搜索。当我看到所有这些愚蠢的编译器错误消息时,我就是这么做的。

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.

神魇的王 2024-08-21 20:51:42

看起来您在 pair 的某处有一个作业。编译器正在尝试将其转换为您列出的声明,但如果没有显式强制转换,它无法从 int 转换为指针。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文