简体   繁体   English

向量迭代器在分配重载中不兼容

[英]Vector iterators incompatible in assignment overload

Simple problem here. 这里的简单问题。 I'm dealing with a bit of an issue of assignment here with vectors. 我在这里处理向量的分配问题。 I have one class, Inventory: 我有一个类,库存:

class Inventory
{
public:
    __inline void operator=( const Inventory& rtSide )
    {
        items.clear();
        for(auto it=rtSide.items.begin(); it!=rtSide.items.end(); ++it)
        {
            items.push_back(*it);
        }
    }
private:
    std::vector<void*> items;
}

There's a struct that contains the class: 有一个包含该类的结构:

typedef struct
{
    Inventory *inventory;
} player_t;

Upon assigning/doing pointer math on a pointer of player_t, Inventory's = overload gets called, as you would expect. 在player_t指针上分配/执行指针数学运算后,如您所料,将调用Inventory的=重载。 However, using MSVC, it would appear that there's an assertion failure, specifically, "vector iterators incompatible". 但是,使用MSVC似乎会出现断言失败,特别是“向量迭代器不兼容”。 The weird thing is, this happens on the clear(). 奇怪的是,这发生在clear()上。 I have no idea what's goin on here. 我不知道这是怎么回事。 If someone could give me some assistance that would be great. 如果有人可以给我一些帮助,那就太好了。

Here's an example of what's going on. 这是一个正在发生的事的例子。 Using pointer math, we can determine the client number by subtracting the current client's player structure from the base: 使用指针数学,我们可以通过从基数中减去当前客户端的玩家结构来确定客户端编号:

clientNum = newcl - svs.clients;

This specifically is what is causing me to assert. 这正是导致我断言的原因。

I'm pretty sure you want to copy the items from the rtSide list, not the ones in this . 我很确定您要复制rtSide列表中的项目,而不是this

    items.clear();
    for(auto it=rtSide.items.begin(); it!=rtSide.items.end(); ++it)
    {
        items.push_back(*it);
    }

(Of course, the compiler will generate this code for you if you don't write your own operator= , and you can also copy a vector by items = rtSide.items; - my point with the post was more to point out that "there is nothing wrong with your iterators, you are just not copying the right thing"). (当然,如果您不编写自己的operator= ,则编译器将为您生成此代码,并且您还可以通过items = rtSide.items;复制向量items = rtSide.items; -我在帖子中的观点更多是指出“迭代器没有任何问题,只是您没有复制正确的内容”)。

And I would also say that using a vector<void *> is pretty much "using C++ in a C way". 我还要说,使用vector<void *>几乎是“以C方式使用C ++”。 You should store at least a pointer to a base-class, if not a smart pointer to a base-class. 如果不是指向基类的智能指针,则至少应存储一个指向基类的指针。 Baseclass could be something like "inventry_item" or something like that. 基类可以是“ inventry_item”之类的东西。

(And I believe xxxx_t types are reserved for POSIX, so you shouldn't really call your types xxxx_t ) (而且我相信xxxx_t类型是为POSIX保留的,因此您不应该真正调用类型xxxx_t

I suspect that your pointer arithmetic resulted in attempting to call the assignment operator on an Inventory , but the pointer did not in fact point to an Inventory . 我怀疑您的指针算法导致尝试调用Inventory上的赋值运算符,但是指针实际上并未指向Inventory (eg it may point to a delete 'd Inventory ) If that was the case, vector 's guts could be filled with random garbage, which could cause these kinds of assertion failures. (例如,它可以指向delete 'd Inventory )如果是这样的情况下, vector的胆可填充有随机垃圾,这可能导致这些种断言故障。

For instance, your the caller might do the moral equivalent of: 例如,您的呼叫者可能在道德上等同于:

Inventory out;
Inventory* target = nullptr;
*target = out;

which could cause these kinds of assertion failures. 可能导致此类断言失败。 Check the code calling the assignment operator. 检查调用赋值运算符的代码。

(Also note that your assignment operator doesn't handle assignment to self; but in that case it will just clear the Inventory , not cause assertion failures) (还要注意,您的赋值运算符不会处理对self的赋值;但是在这种情况下,它只会清除Inventory ,而不会导致断言失败)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM