[英]C++ “Unable to read memory” when accessing pointer object from inherited class
[英]c++ unable to read memory with custom class when calling a function
我在自定義類中運行函數時遇到問題。 這是我的代碼:
機器人結構
#pragma once
#include "math.h"
#include <fstream>
#include <string>
#include <thread>
#include <chrono>
#include <ctime>
#include <vector>
#include <sstream>
#include <fstream>
using namespace std;
using namespace std::chrono;
class RobotStructure
{
bool faceTrackingEnabled;
public:
RobotStructure();
~RobotStructure();
bool testFunction(string input);
機器人結構
#include "RobotStructure.h"
RobotStructure::RobotStructure()
{
faceTrackingEnabled = true;
}
RobotStructure::~RobotStructure()
{
}
bool RobotStructure::testFunction(string input)
{
cout << input << endl; //THIS DOES NOT WORK, When using debugger shows that the entire class "Robot Structure" as unable to read memory
return true;
}
主要
#include <Windows.h>
#include <iostream>
#include <iomanip>
#include <ctime>
#include <chrono>
#include <thread>
#include <string>
#include <sstream>
#include <vector>
#include <math.h>
#include <fstream>
#include "RobotStructure.h"
using namespace std;
int main()
{
RobotStructure *mybot= new RobotStructure();
mybot->testFunction("test");
return 0;
}
如果在main中設置了斷點,則我的類將正確初始化,並且一切都很好。 一旦調用“測試功能”,該類就會從測試功能內的內存中丟失。
當返回main之前,返回0;否則,返回0。 該類也內存不足。 好像指針以某種方式被刪除。 有人可以解釋發生了什么,我做錯了什么?
您將該函數聲明為bool testFunction(string input)
但該函數未返回任何內容。 這導致C ++中的未定義行為。
另外,您的示例無法編譯(您聲明了參數input
但您正在使用intput
)。
您的testFunction
不使用任何類成員。 如果您編譯此代碼啟用優化(在VisualStudio中“釋放” -構型-O2
海合會),編譯器可能會導致某些變量(如this
)“undebuggable”。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.