简体   繁体   English

通过地址将向量的元素传递到不同的数据结构

[英]Passing the elements of a vector to a different data structure by address

I am building a 2d game and I am storing all my enemy objects in an array. 我正在构建2D游戏,并将所有敌人对象存储在一个数组中。 Right now I am trying to implement a quadtree . 现在,我正在尝试实现四叉树 Currently I am just trying to build the quadtree and am not concerned with collisions. 目前,我只是在尝试构建四叉树,并不关心碰撞。 The code that pushes items to the quadtree is the following : 将项目推送到四叉树的代码如下:

for (std::vector<Enemy>::iterator i=m_enemies.begin(); i != m_enemies.end(); ++i) {
    std::cout << &(*i) << "Address of the object" << std::endl;
    m_quad.Insert(&(*i));
}

The code for the Insert is the following : 插入的代码如下:

void Quad::Insert(sf::RectangleShape* l_gameObject){
    std::cout << &l_gameObject << "dsa1" << std::endl;
    std::cout << "called insert " << m_objects.size() << std::endl;
    m_objects.push_back(l_gameObject);
    if (m_level < m_maxLevel) {
        if (m_objects.size() > 3) {
            std::cout<< "creating subregions " << m_objects.size() << std::endl;
            m_subRegions.push_back(Quad(m_x,m_y,m_width/2.f, m_height/2, m_level + 1, m_maxLevel-1));
            m_subRegions.push_back(Quad(m_x+m_width/2.f,m_y,m_width/2.f,m_height/2.f, m_level + 1, m_maxLevel-1));
            m_subRegions.push_back(Quad(m_x+m_width/2.f, m_y + m_height/2.f, m_width/2.f, m_height/2.f, m_level + 1, m_maxLevel-1));
            m_subRegions.push_back(Quad(m_x, m_y + m_height/2.f, m_width/2.f, m_height/2.f, m_level + 1, m_maxLevel-1));
            std::vector<int> temp;
            for (int i=0; i < m_objects.size(); i++){
                for (int j=0; j< m_subRegions.size(); j++) {
                    if (m_subRegions[j].Contains(m_objects[i])) {
                        m_subRegions[j].Insert(m_objects[i]);
                        temp.push_back(i);
                        break;
                    }
                }
            }
            for (int i = temp.size(); i > -1; i--){
                m_objects.erase(m_objects.begin() + temp[i]);
            }
        }
    }
}

When I print the address that I am passing to the Insert function and the one I have in the function I see that they are different. 当我打印,我传递插入函数和一个我函数的地址我看到他们是不同的。 In fact the on in is always the same and the one I pass is always different as it should be. 事实上, 总是相同的和一个我通过始终是不同的,因为它应该是。 Could anyone clarify why that is the case ? 任何人都可以澄清为什么会这样吗?

EDIT : Thanks to gsamaras for pointing out that I was printing the address of the parameter. 编辑:感谢gsamaras指出我正在打印参数的地址。

Followup question 后续问题

When I use the methods of the object I am addressing in the first for loop I get the correct results, but when I do the same thing in the Insert function I get 0. Why is that ? 当我在第一个for循环中使用要处理的对象的方法时,会得到正确的结果,但是当我在Insert函数中执行相同的操作时,我会得到0。为什么?

Inside Insert , you're printing the address of the parameter. Insert ,您正在打印参数的地址。
Outside Insert , you're printing the parameter's value. Insert外部,您正在打印参数的值。

You want 你要

std::cout << l_gameObject << "dsa1" << std::endl;

since l_gameObject is the address you're passing in. 因为l_gameObject是您要传递的地址。

You are printing the address of the address. 您正在打印地址的地址。

Change this: 更改此:

std::cout << &l_gameObject << "dsa1" << std::endl;

to this: 对此:

std::cout << l_gameObject << "dsa1" << std::endl;

in order to print the same thing as outside your of your function. 为了在您的功能之外打印相同的内容。

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

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