简体   繁体   English

当我尝试调用指针方法时代码卡住了

[英]Code gets stuck when I try to call a pointer method

I may or may not be dumb, but I don't see any reason why my code should work like this instead of how it's supposed to, but any explanation is welcome, as I'm completely lost in my assignment.我可能愚蠢也可能不愚蠢,但我看不出我的代码应该像这样工作而不是应该如何工作的任何理由,但欢迎任何解释,因为我完全迷失在我的任务中。

The part where I run into a problem is this part:我遇到问题的部分是这部分:

In this part, the problem occurs in the inner for in the do while loop, legretegek is a vector<Legreteg*> , viszonyokall is a string, that I give to each simulate method.在这部分中,问题出现在 do while 循环的内部 for 中, legretegek是一个vector<Legreteg*> , viszonyokall 是一个字符串,我将其提供给每个模拟方法。 The problem is, that it only runs for the first i value, after that it fails.问题是,它只运行第一个i值,之后它失败了。 And it returns with -1073741819.它返回 -1073741819。

And last the Legreteg.cpp which contains only the simulate methods:最后是仅包含模拟方法的 Legreteg.cpp:

#include "Legreteg.h"

using namespace std;

const char zivatar = 'z'; // ozon : - , oxigen : 50% ozon , szendioxid : -
const char napos = 'n'; // ozon : - , oxigen : 5% ozon , szendioxid : 5% oxigen
const char mas = 'm'; // ozon : 5% oxigen , oxigen : 10% szendioxid , szendioxid : -

void Ozon::simulate(char &viszony){
    cout << "eljut az ozon simulateig"<<std::endl;
    double newvastagsag = vastagsag;
    switch (viszony){
        case mas: vastagsag *= 0.95; cout << "mas"; *alakul = Oxigen(newvastagsag*0.05, "Oxigen"); cout << "mas2"; break;
        default: break;
    }
}

void Oxigen::simulate(char &viszony){
    cout << "eljut az oxigen simulateig"<<std::endl;
    double newvastagsag = vastagsag;
    switch (viszony){
        case zivatar: vastagsag *= 0.50; cout << "zivatar"; *alakul =Ozon(newvastagsag*0.50, "Ozon"); cout << "zivatar2"; break;
        case napos: vastagsag *= 0.95; cout << "napos"; *alakul = Ozon(newvastagsag*0.05, "Ozon"); cout << "napos2"; break;
        case mas: vastagsag *= 0.90; cout << "mas"; *alakul = Szendioxid(newvastagsag*0.10, "Szendioxid"); cout << "mas2"; break;
    }
}

void Szendioxid::simulate(char &viszony){
    cout << "eljut a szendioxid simulateig"<<std::endl;
    double newvastagsag = vastagsag;
    switch (viszony){
        case napos: vastagsag *= 0.95; cout << "napos"; *alakul = Oxigen(newvastagsag*0.05, "Oxigen");cout << "napos2"; break;
        default: break;
    }
}

The main part gets as far as generating a new Legreteg* object into *alakul , but after that, the cout I placed behind it for torubleshoot reasons, doesn't get ran anymore, and this is the point when the program returns with -1073741819.主要部分是在 *alakul 中生成一个新的Legreteg* *alakul ,但在那之后,我出于解决问题的原因放在它后面的 cout 不再运行,这就是程序返回 -1073741819 的点.

Sorry if it's too much, but I'm completely lost on the problem, and any help is appreciated.对不起,如果它太多了,但我完全迷失了这个问题,感谢任何帮助。

A test input(put this in an input.txt, and write input.txt into the program):一个测试输入(把它放在一个 input.txt 中,并将 input.txt 写入程序中):

4
z 5
x 0.4
s 3
x 4
mmmmnnznnmm

There are no lines that sets the member variable alakul to values other than nullptr .没有任何行将成员变量alakul设置为nullptr以外的值。 Then you dereferenced alakul ( nullptr ) in the function simulate() like:然后你在 function simulate()中取消引用alakulnullptr ),如:

*alakul = Oxigen(newvastagsag*0.05, "Oxigen");

This is wrong.这是错误的。

Instead of this, I guess you wanted to allocate new object and assign that to alakul like而不是这个,我猜你想分配新的 object 并将其分配给alakul

alakul = new Oxigen(newvastagsag*0.05, "Oxigen");

instead of dereferencing alakul there.而不是在那里取消引用alakul

Also you may want to define你也可能想定义

  • Copy constructors复制构造函数
  • Assignment operators赋值运算符
  • Destructors析构函数

to handle the pointer properly on object copying.在 object 复制时正确处理指针。 Be careful not to violate The Rule of Three .注意不要违反三法则

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

相关问题 当我尝试将 char 输入到 int 时,为什么文件指针会卡住? - Why does the file pointer stuck when I try to input char to int? 代码在不使用 cout 时卡住并且不适用于某些数字 - Code gets stuck when not using cout and doesnt work with some numbers 当我尝试释放指针向量时出现问题 - Problem when I try to free a pointer vector 从C程序调用C ++ DLL时,当我尝试访问DLL中的调用参数指针时会崩溃 - When calling a C++ DLL from a C program it crashes when I try to access call parameter pointer in the DLL 当我尝试函数重载时,数组获取垃圾值 - Array gets garbage value when i try function overloading 当我尝试为结构分配新指针时出现堆栈溢出错误 - Stack overflow error when i try to assign a new pointer to a structure 当我尝试使用CComObject函数创建实例时,_pAtlModule指针为null - _pAtlModule pointer is null when i try to CreateInstance using the CComObject function 当我尝试删除指针时我的程序崩溃 - My program crashes when I try to delete a pointer 当我尝试将索引分配给 nullptr 时,指向 Array 抛出错误的指针 - Pointer to Array throwing errors when I try to assign an index to nullptr 当我尝试使用指针的别名时,g++ 返回错误 - g++ returning errors when i try to use an alias of a pointer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM