0xc00000005:访问冲突读取位置0x00000008

时间:2019-01-22 16:49:52

标签: pointers vector

我试图将类推入一个矢量trps,将所有部队存储在函数level::Init()中,但是它崩溃了并报告了这一点。

  

访问冲突读取位置0x00000008。

我只是想不通。

level.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;
};

level.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

            }
        }
    }
}

troops.h

#pragma once

class troops
{
public:
    troops(char side, int y, int x, int health);
private:
    char Side;
    int Y;
    int X;
    int Health;
};

0 个答案:

没有答案