pointers - 0xc00000005:访问冲突读取位置0x00000008
问题描述
我尝试将 push_back 类推送到一个向量 trps,该向量 trps 将所有部队存储在函数中level::Init()
,但它崩溃并报告了这一点。
访问冲突读取位置 0x00000008。
我就是想不通。
级别.h
#pragma once
#include <fstream>
#include <vector>
#include "troops.h"
using namespace std;
class level
{
public:
level(string Filename);
void print();
void Init();
private:
vector <string> Level;
vector <troops> *trps;
};
级别.cpp
#include "level.h"
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include "troops.h"
using namespace std;
level::level(string Filename)
{
fstream file;
file.open(Filename);
if (file.fail()){
cout << "Failed To Load Game!" << endl;
perror(Filename.c_str());
file.close();
}
else{
//Check if level isnt clear
if (!Level.empty()){
Level.clear();
}
//psuh back to level
string line;
while (getline(file, line)){
Level.push_back(line);
}
file.close();
}
}
void level::print(){
for (int i = 0; i < Level.size(); i++){
cout << Level[i] << endl;
}
}
void level::Init(){
//vector of pointer that store troops
for (int i = 0; i < Level.size(); i++){
for (int x = 0; x < Level[i].size(); x++){
if (Level[i][x] != ' ' && Level[i][x] != '#'){
trps->push_back(troops(Level[i][x], i, x, 100)); <-THIS LINE
}
}
}
}
部队.h
#pragma once
class troops
{
public:
troops(char side, int y, int x, int health);
private:
char Side;
int Y;
int X;
int Health;
};
解决方案
推荐阅读
- elasticsearch - 未执行弹性搜索查询
- amazon-web-services - 您可以将 AWS Cloudfront 设置为在同一个分发中具有受限和公共文件吗
- python - 是否可以从网页中获取源代码并使用 selenium + python 进行更改?
- c# - 如何在asp.net c#中更改开关切换时更新数据库
- python - 对于 TensorRT 版本 5.1.5,模块“tensorrt”没有属性“Logger”
- git - 允许非 GitHub 用户通过密码克隆私有仓库
- java - JavaFX、Scene Builder (IntelliJ) 程序从 StackOverFlow 抛出 java.lang.reflect.InvocationTargetException
- firebase - 从谷歌表格脚本重新排列在firebase实时数据库中显示的值
- html - 表格元素行左侧的图标?
- svg - Inkscape:持久对齐