从 actorAwarding.cpp 开始,我们将一个 actor 对象添加到 actor db,一个包含 BST、最大堆和动态分配数组的类
void do_register_actor(int actor_id, string actor_last, string actor_first)
{
Actor a(actor_id, actor_last, actor_first);
if (actordb.addActor(a))
{
Actor *actor_ptr = &a;
actordb.actor_to_heap(actor_ptr);
cout << "register_actor: Registered actor " << a.getName() << endl;
}
else
cout << "register_actor: Error actor id " << actor_id << " already in use" << endl;
return;
}
在ActorDB.cpp中,我们添加一个指针对象到最大堆(actors_award)
void ActorDB::actor_to_heap(Actor *actor)
{
actors_award.insert(actor);
}
我们还有 addActor 将其添加到 BST,我在这里使用它,但决定创建一个单独的函数,无论问题仍然存在
bool ActorDB::addActor(Actor &actor)
{
// Actor *ptr = &actor;
// actors_award.insert(actor);
unsigned int actorID = actor.getID();
string lastname = actor.getLastName();
unsigned int insertPosition = 0;
for (unsigned int i = 0; i < actors.getSize(); i++)
{
if (actors[i].getID() == actorID)
{
return false;
}
if (actors[i].getID() > actorID)
{
insertPosition = i;
break;
}
insertPosition++;
}
cout << insertPosition << endl;
/*
Node* bstRoot = actor_tree.getRoot();
if (!bstRoot) {
actor_tree.setRoot(actor_tree.BSTInsert(bstRoot, lastname, insertPosition));
} else {
actor_tree.BSTInsert(bstRoot, lastname, insertPosition);
}
Node* searchedNode = actor_tree.search(bstRoot, lastname);
actor.set_BSTcounterpart(searchedNode);
if (insertPosition == actors.getSize()) {
actors.add(actor);
} else {
actors.addAtActors(insertPosition, actor);
}
*/
if (insertPosition == actors.getSize())
{
if (!actor_tree.getRoot())
{
actor_tree.setRoot(actor_tree.BSTInsert(actor_tree.getRoot(), lastname, insertPosition));
}
else
{
actor_tree.BSTInsert(actor_tree.getRoot(), lastname, insertPosition);
}
// Search for the node in the BST to get the counterpart
Node *searchedNode = actor_tree.search(actor_tree.getRoot(), lastname);
// cout << searchedNode->index << " please" << endl;
actor.set_BSTcounterpart(searchedNode);
// cout << actor.get_BSTcounterpart()->index << " something " << endl;
// Add the actor to the dynamic array
actors.add(actor);
// actors_award.insert(actor);
}
else
{
if (!actor_tree.getRoot())
{
actor_tree.setRoot(actor_tree.BSTInsert(actor_tree.getRoot(), lastname, insertPosition));
}
else
{
actor_tree.BSTInsert(actor_tree.getRoot(), lastname, insertPosition);
}
Node *searchedNode = actor_tree.search(actor_tree.getRoot(), lastname);
actor.set_BSTcounterpart(searchedNode);
actors.addAtActors(insertPosition, actor);
// actors_award.insert(actor);
}
// Actor* ptr = *actors
return true;
}
接下来我们有最大堆,这就是问题发生的地方,我最初引用了 actor 对象,但无论如何问题仍然存在。我显然对最大堆有其他功能,但我现在只使用插入来解决问题,因为问题仍然存在于完整功能中
void maxHeap::insert(Actor *actorb)
{
//cout << actorb -> getName() << endl;
//Actor *actor_ptr = &actor;
// problem still persisted when i initalized a pointer inside insert ^
max_heap.add(actorb);
// cout << " in insert " << actor.getName() << " size:" << max_heap.getSize() << endl;
if (max_heap.getSize() == 1)
{
return;
}
}
max_heap 是一个来自模板类 temp_arr 的动态分配数组,在这个类中我只使用 add 和 op[],我的容量从 10 开始,但输入的对象不超过 10 个,所以不包括调整大小
template<typename T>
void temp_arr<T>::add( T& element) {
// Add implementation
// Add an element to the array, resizing if necessary
if(size == capacity - 1 ){
resize();
}
array[size] = element;
size++;
}
template<typename T>
T& temp_arr<T>::operator[](unsigned int index){
if (index >= size) {
// Out-of-bounds check
throw std::out_of_range("Index out of range");
// Alternatively, you can handle this case differently, like returning a default value or throwing an exception
}
return array[index];
}
我尝试过完善复制构造函数并将广告放入最大堆中不同函数的堆中,但问题仍然存在。有什么奇怪的:
1 特拉沃尔塔 在 actor_to_heap 中,我们正在尝试添加当前演员:约翰·特拉沃尔塔
地址: 0000028645781ab0 约翰·特拉沃尔塔
在堆插入中,大小:1 地址: 0000028645781ab0 约翰·特拉沃尔塔
地址: 0000028645781ab8 约翰·特拉沃尔塔 register_actor: 注册演员约翰·特拉沃尔塔
:2 着陆器 在 actor_to_heap 中,我们正在尝试添加当前的 actor:home lander
地址: 0000028645781ab0 着陆器
地址: 0000028645781ab8 家用着陆器 在堆插入中,大小:2
地址: 0000028645781ab0 着陆器 地址:
0000028645781ab8 家用着陆器 地址:
0000028645781ac0 家庭着陆器
register_actor:注册演员主页登陆器 :3
赫本 IN actor_to_heap,我们正在尝试添加的当前演员:奥黛丽·赫本 地址:
0000028645781ab0 奥黛丽·赫本 地址:
0000028645781ab8 奥黛丽·赫本 地址:
0000028645781ac0 奥黛丽·赫本 在堆插入中,大小:3 地址:
0000028645781ab0 奥黛丽·赫本 地址:
0000028645781ab8 奥黛丽·赫本 地址:
0000028645781ac0 奥黛丽·赫本 地址:
0000028645781ac8 奥黛丽·赫本
当我打印 actor_to_heap 中的 max_heap (将 actor 添加到 actorDB 中的最大堆)时,max_heap 中的指针已经指向该 actor。这怎么可能?我确信我不会添加到其他任何地方,我确信我是三层甲板。我的 temp_arr 也适用于 BST 和 actorDB 中的数组。有什么想法吗?
例如,如果您在函数内声明变量 x,则 x 仅在该函数体内可见。
https://learn.microsoft.com/en-us/cpp/cpp/scope-visual-cpp?view=msvc-170
因此,您在函数内创建了本地
a
变量,因此它具有本地作用域:
局部作用域 在函数或 lambda 中声明的名称(包括参数名称)具有局部作用域。他们通常被称为“当地人”。它们仅从声明点到函数或 lambda 体的末尾可见。本地作用域是一种块作用域,本文稍后将对此进行讨论。
当你的函数退出时,你的局部变量会被销毁,因此像你一样返回指针是未定义的行为,并且会让你头疼。因此,这是不好的做法,您应该使用对象而不是指向所述对象的无主指针。
我想回到使用对象,我切换是因为我还有其他功能,我必须更改 Actor 对象中的数据成员
那就是你真正的问题了。您想按照您所描述的方式解决它,但现在您知道这种方式是无效的,您应该回到出发点,找到一个不同的、有效的解决方案来解决这个问题。
我想知道您最初想要改变什么。如果您只谈论数据成员的值,那么这在 C++ 中并不是真正的问题。但是,如果您谈论您的数据成员是什么,那么这听起来像是糟糕的设计。你可能会创建另一个类或其他类,你可能会使用继承等。所以你的问题的答案是你当前解决问题的方法是无效的,所以你越早回到你的出发点,对你来说越好。