C ++构造函数获取xtree错误

时间:2014-05-22 20:51:37

标签: c++ inheritance map

我正在尝试构建我的一个类,但是我遇到了一个xtree违规错误。 这是我如何构建我的类

Game.h

#pragma once

#include "ImageService.h"

class Game
{
private:
    IntroState introState;

public:
    ImageService imageService;

    Game();
    ~Game();
};

game.cpp

Game::Game() : introState(imageService)
{

}

然后转到introState

IntroState.h

pragma一次

#include "State.h"

class IntroState : public State
{
public:
    IntroState(ImageService &imageService);
    ~IntroState();

    GameState objectState;
};

IntroState.cpp

#include "IntroState.h"


IntroState::IntroState(ImageService& imageService) : State (imageService)
{

}


IntroState::~IntroState()
{
}

然后转到State

State.h

#pragma once

#include "ImageService.h"

class State
{
private:
    ImageService imageService;
public:
    State( ImageService& is );
    ~State();

};

State.cpp

#include "State.h"


State::State(ImageService& _imageService)
{
    imageService = _imageService;
}


State::~State()
{
}

然后转到图像服务的构造函数

ImageService.h

#pragma once

#include <map>
#include "Enums.h"
#include "Sprite.h"

class ImageService
{
public:
    ImageService();
    ~ImageService();

    std::map<ImageName, Sprite> Images;
};

ImageService.cpp

ImageService::ImageService()
{
    Images = std::map<Image, sf::Texture>();
}


ImageService::~ImageService()
{
}

现在这里是思考和崩溃的步骤

1 : Game constructor
2 : IntroState constructor
3 : State constructor
4 : ImageService constructor
5 : Images are then set to an empty map
6 : Trying to assign ImageService reference to State
7 : Xtree error shown (Unhandled exception at 0x012BF147 in Dungeon_Wraight.exe: 0xC0000005: Access violation reading location 0x00000004.)

这是xtree中的功能

template<class _Moveit>
        void _Copy(const _Myt& _Right,
            _Moveit _Movefl)
        {   // copy or move entire tree from _Right
        _Root() = _Copy_nodes(_Right._Root(), this->_Myhead, _Movefl); //Here is the error
        this->_Mysize = _Right.size();
        if (!this->_Isnil(_Root()))
            {   // nonempty tree, look for new smallest and largest
            _Lmost() = this->_Min(_Root());
            _Rmost() = this->_Max(_Root());
            }
        else
            {   // empty tree, just tidy head pointers
            _Lmost() = this->_Myhead;
            _Rmost() = this->_Myhead;
            }
        }

为什么我得到这个错误?

1 个答案:

答案 0 :(得分:0)

Xtree 实现了std::setstd::map的某些内存和时间功能。知道这一点,错误可能会出现在代码的2个地方:

    在创建ImageService实例之前未在Game中创建
  1. IntroState实例
  2. 错误的images初始化。如果我在 SFML sf::Texture中正确了解,请不要继承Sprite。这是sprite的成员。