迭代器打印两个值,一个是文本,另一个是指针

Iterator prints two values, one is text and other is unfortunately a pointer

本文关键字:指针 文本 另一个 一个 打印 两个 迭代器      更新时间:2023-10-16

下面的代码显示了四条消息,前两条是正常的,另外两条通过迭代器,但第二条没有显示正确的值,而是打印指针而不是值,但这两个变量的代码几乎相同。

代码很大,重要部分在main和show_message内部,问题是anm->getName()有效,而annm->getParent()->getName(

#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <stdlib.h>
#include <malloc.h>
#include <memory.h>
#include <tchar.h>
#include <string>
#include <vector>
class Node;
class CustomObject;
class CustomObject
{
    public:
    std::string name;
    Node* parent;
    CustomObject(std::string n_name);
    std::string getName();
    void setParent(Node* pparent);
    Node* getParent();
};
CustomObject::CustomObject(std::string n_name)
{
    name = n_name;
}
std::string CustomObject::getName()
{
    return name;
}
void CustomObject::setParent(Node* pparent)
{
    parent = pparent;
}
Node* CustomObject::getParent()
{
    return parent;
}
class Node
{
public:
    std::string name;
    Node* parent;
    std::vector<Node> childs;
    Node(std::string n_name);
    void attach(Node* nd);
    void attach(CustomObject* anm);
    std::string getName();
    void setParent(Node* pparent);
    Node* getParent();
    std::vector<CustomObject> mCustomObjects;
};
Node::Node(std::string n_name)
{
    name = n_name;
}
std::string Node::getName()
{
    return name;
}
void Node::setParent(Node* pparent)
{
    parent = pparent;
}
Node* Node::getParent()
{
    return parent;
}
void Node::attach(Node* nd)
{
    childs.push_back(*nd);
    nd->setParent(this);
}
void Node::attach(CustomObject* anm)
{
    mCustomObjects.push_back(*anm);
    anm->setParent(this);
}
class Game
{
    std::string name;
public:
    std::vector<Node> nodes;
    std::vector<CustomObject> mCustomObjects;
    Game(std::string nm);
};
Game::Game(std::string nm)
{
    name = nm;
}
void show_message(CustomObject* anm)
{
    MessageBox( NULL, anm->getName().c_str(),"Message", 0);
    MessageBox( NULL, anm->getParent()->getName().c_str(),"Message", 0);
}
int main()
{
    Game* mGame = new Game("MyGame");
    Node* mNode = new Node("MyNode");
    mGame->nodes.push_back(*mNode);
    CustomObject* mCustomObject = new CustomObject("Object1");
    mGame->mCustomObjects.push_back(*mCustomObject);
    mNode->attach(mCustomObject);
    show_message(mCustomObject);
    for (std::vector<CustomObject>::iterator itr = mGame->mCustomObjects.begin(); itr != mGame->mCustomObjects.end(); ++itr)
    {
        CustomObject* cObj;
        cObj = &(*itr);
        show_message(cObj);
    }
}

那么,你知道这个代码出了什么问题吗?

谢谢。

这里有一个问题,基本上是程序的症状:

void Node::attach(CustomObject* anm)
{
    mCustomObjects.push_back(*anm);
    anm->setParent(this);
}

您正在调用此函数,但请仔细查看它。第一行取消指针的引用,并在向量中放置CustomObject副本。下一行设置父对象,而不是放置在矢量中的副本,而是传入的指针。

因此,最终,你的向量会得到带有伪父指针的副本。你很幸运,你能看到任何输出。

删除的Windows内容的崩溃示例

除此之外,在不同的地方还会发生内存泄漏。您的代码根本不调用delete

总的问题似乎是你过度使用了指针,以至于你不确定是使用值类型还是指针。我建议您放弃所有这些指针主义(或大部分),并使用值。只有在确定是的情况下,才应该使用指针。即使使用指针,也要尝试使用智能指针(std::unique_ptrstd::shared_ptr等)