0xc00000005: Access violation reading location 0x00000008

0

I tried to push_back class to a vector trps which store all the troops in function level::Init() but it crashed and reported this.

Access violation reading location 0x00000008.

I just cant figure it out.

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;
};
pointers
vector
asked on Stack Overflow Jan 22, 2019 by spashz • edited Jan 22, 2019 by Gilles-Antoine Nys

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0