简体   繁体   English

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

[英]C++ constructor gets xtree error

I'm trying to construct one of my classes but i'm getting a xtree violation error.我正在尝试构建我的一个类,但出现 xtree 违规错误。 Here is how I am constructing my classes这是我构建课程的方式

Game.h游戏.h

#pragma once

#include "ImageService.h"

class Game
{
private:
    IntroState introState;

public:
    ImageService imageService;

    Game();
    ~Game();
};

game.cpp游戏.cpp

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

}

This then goes to introState然后进入 introState

IntroState.h介绍状态.h

pragma once pragma once

#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()
{
}

This then goes to State然后进入状态

State.h状态.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()
{
}

This then goes to the constructor of the image service然后转到影像服务的构造函数

ImageService.h图像服务.h

#pragma once

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

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

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

ImageService.cpp图像服务.cpp

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


ImageService::~ImageService()
{
}

Now here are the steps it goes thought and crashes现在这是它思考和崩溃的步骤

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

This is the function in xtree这是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;
            }
        }

Why on earth am I getting this error?为什么我会收到这个错误?

Xtree implement some memory and time features of std::set and std::map . Xtree实现了std::setstd::map一些内存和时间特性。 Knowing this, error can be appeared in 2 places of your code:知道这一点,错误可能出现在代码的 2 个地方:

  1. ImageService instance don't created in Game before creating a IntroState instance ImageService情况下不创建Game创建之前IntroState实例
  2. Incorrect images initialization. images初始化不正确。 If I know correctly in SFML sf::Texture don't inherit Sprite .如果我在SFML 中正确地知道sf::Texture就不要继承Sprite This is a member of sprite.这是精灵的成员。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM