迭代器打印两个值,一个是文本,另一个是指针
Iterator prints two values, one is text and other is unfortunately a pointer
下面的代码显示了四条消息,前两条是正常的,另外两条通过迭代器,但第二条没有显示正确的值,而是打印指针而不是值,但这两个变量的代码几乎相同。
代码很大,重要部分在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_ptr
、std::shared_ptr
等)
相关文章:
- 在指针的帮助下,文本文件中单词的频率
- 将字符指针十六进制转换为字符串并保存在文本文件C++中
- 为什么多维数组中的空字符串文本衰减为空指针?
- 基于模板字符串文本返回的 c++ 函数指针
- 指针类型是文本类型吗?
- 打印char指针的Unicode和文本值
- 在此任务上遇到麻烦,我们需要使用指针和数组来找到文本文件中某些数字的平均值,中值和模式
- C++ ->尝试逐字阅读一行文本。如何使指针等效于用于存储输入的当前二维数组
- 如何在文本文件中逐行读取并填充指向对象数组的指针
- 如何从文本文件中填充指针数组
- 我可以将指向字符指针的指针直接分配给字符串文本吗?
- G++ 将返回的字符串文本视为常量字符指针而不是常量字符数组
- 将文本文件转换为常量字符指针
- 编译器未将指向字符串文本的良好指针解释为方法的参数
- 声明并初始化指针后,何时在变量文本前面加上星号,何时不作为前缀
- 如何正确地将一个文本整数作为参数作为空指针进行传递,并使用其地址作为整数的实际值
- 为什么将整数文本类型转换为指针值会导致非常量表达式
- 编写一个函数,将字符串文本复制到指针给出的 char 数组中
- 将指向字符串文本的指针返回给调用方是否安全
- 迭代器打印两个值,一个是文本,另一个是指针