C ++指针运行时错误-使用指针设置变量然后检索

SBUK技术

我正在设置一个原型c ++控制台应用程序。该程序包含一些虚拟类和指针等。当程序在主要功能中到达下面的代码行时,它将崩溃。我相信这与访问该指针处的内存有关。

主要()

...
Player _player();  //new player object created
Actor *player = &_player;  //pointer to player created

...
//note player and get_inventory() are/return a pointer
{
 Inventory* a =  player->get_Inventory();
 a->set_testMe("testedMe");
 string result = a->get_testMe();
 cout << result << endl;
}

{
 Inventory* a =  player->get_Inventory();
 string result = a->get_testMe();  //This causes error
 cout << result << endl;
}
...

Actor.cpp // get_Inventory()

...
Inventory* Actor::get_Inventory()
{
    Inventory mInventory = this->actorInventory;
    Inventory * pInventory = &mInventory;
    return pInventory;
}
...

库存文件

...
Inventory::Inventory()
{
this->testMe = "initial test";
}

void Inventory::set_testMe(string input)
{
    this->testMe = input;
}
string Inventory::get_testMe()
{
    return this->testMe;
}
...

有任何想法吗?谢谢

乔·Z

这将返回一个指向局部变量的指针:

Inventory* Actor::get_Inventory()
{ 
    Inventory mInventory = this->actorInventory;
    Inventory * pInventory = &mInventory;
    return pInventory;
}

第一条语句复制this->actorInventory到局部变量(如方法局部get_Inventory),然后返回指向该局部变量的指针。从中返回后get_Inventory(),该变量将超出范围,不再存在。

您可能想要尝试this->actorInventory直接返回指向的指针

Inventory *Actor::get_Inventory()
{
    return &actorInventory;
}

或者,如果您不希望调用者修改actorInventory,请返回一个const合格的指针:

const Inventory *Actor::get_Inventory() const
{
    return &actorInventory;
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

C ++指针运行时错误

来自分类Dev

C ++中的指针和运行时错误

来自分类Dev

如何在C程序(不是C ++)中处理运行时内存分配错误的指针错误?

来自分类Dev

指向指针的ANSI C内存分配引发非致命的运行时错误

来自分类Dev

指向指针的ANSI C内存分配引发非致命的运行时错误

来自分类Dev

返回指针时的运行时错误

来自分类Dev

运行时错误,使用未声明的变量,C++

来自分类Dev

尝试使用指针运行时“未命名类型”错误

来自分类Dev

循环和运行时错误中的C ++变量声明

来自分类Dev

重新分配无效的指针运行时错误

来自分类Dev

空指针异常导致运行时错误

来自分类Dev

取消引用char指针运行时错误

来自分类Dev

使用C在Spoj中运行时NZEC错误

来自分类Dev

C-使用char str()函数的运行时错误

来自分类Dev

如何将C ++ / CX运行时对象转换为本机C指针类型?

来自分类Dev

如何在C ++中的运行时中声明n阶指针

来自分类Dev

指向C99之前运行时确定大小的数组的指针

来自分类Dev

在运行时将指针推送到向量 C++

来自分类Dev

总线错误c ++使用小数组正确运行,使用大数组运行时错误

来自分类Dev

总线错误c ++使用小数组正确运行,使用大数组运行时错误

来自分类Dev

Golang Cgo:恐慌:运行时错误:cgo参数具有指向Go指针的Go指针

来自分类Dev

类运行时为空指针

来自分类Dev

空指针运行时异常

来自分类Dev

类型指针的运行时创建?

来自分类Dev

使用结构指针数组时发生free()运行时错误

来自分类Dev

基本的C ++错误。运行时检查失败#2-变量'matrix'周围的堆栈已损坏

来自分类Dev

运行时错误:无效的内存地址或nil指针取消引用

来自分类Dev

Golang:恐慌:运行时错误:无效的内存地址或nil指针取消引用

来自分类Dev

运行时错误:无效的内存地址或分页时取消了nil指针的引用

Related 相关文章

  1. 1

    C ++指针运行时错误

  2. 2

    C ++中的指针和运行时错误

  3. 3

    如何在C程序(不是C ++)中处理运行时内存分配错误的指针错误?

  4. 4

    指向指针的ANSI C内存分配引发非致命的运行时错误

  5. 5

    指向指针的ANSI C内存分配引发非致命的运行时错误

  6. 6

    返回指针时的运行时错误

  7. 7

    运行时错误,使用未声明的变量,C++

  8. 8

    尝试使用指针运行时“未命名类型”错误

  9. 9

    循环和运行时错误中的C ++变量声明

  10. 10

    重新分配无效的指针运行时错误

  11. 11

    空指针异常导致运行时错误

  12. 12

    取消引用char指针运行时错误

  13. 13

    使用C在Spoj中运行时NZEC错误

  14. 14

    C-使用char str()函数的运行时错误

  15. 15

    如何将C ++ / CX运行时对象转换为本机C指针类型?

  16. 16

    如何在C ++中的运行时中声明n阶指针

  17. 17

    指向C99之前运行时确定大小的数组的指针

  18. 18

    在运行时将指针推送到向量 C++

  19. 19

    总线错误c ++使用小数组正确运行,使用大数组运行时错误

  20. 20

    总线错误c ++使用小数组正确运行,使用大数组运行时错误

  21. 21

    Golang Cgo:恐慌:运行时错误:cgo参数具有指向Go指针的Go指针

  22. 22

    类运行时为空指针

  23. 23

    空指针运行时异常

  24. 24

    类型指针的运行时创建?

  25. 25

    使用结构指针数组时发生free()运行时错误

  26. 26

    基本的C ++错误。运行时检查失败#2-变量'matrix'周围的堆栈已损坏

  27. 27

    运行时错误:无效的内存地址或nil指针取消引用

  28. 28

    Golang:恐慌:运行时错误:无效的内存地址或nil指针取消引用

  29. 29

    运行时错误:无效的内存地址或分页时取消了nil指针的引用

热门标签

归档