【问题标题】:C++ constructor gets xtree errorC++ 构造函数得到 xtree 错误
【发布时间】:2014-05-22 20:51:36
【问题描述】:

我正在尝试构建我的一个类,但出现 xtree 违规错误。 这是我构建课程的方式

游戏.h

#pragma once

#include "ImageService.h"

class Game
{
private:
    IntroState introState;

public:
    ImageService imageService;

    Game();
    ~Game();
};

游戏.cpp

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

}

然后进入 introState

IntroState.h

编译指示一次

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

然后进入状态

状态.h

#pragma once

#include "ImageService.h"

class State
{
private:
    ImageService imageService;
public:
    State( ImageService& is );
    ~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;
            }
        }

我到底为什么会收到这个错误?

【问题讨论】:

  • 什么是SpriteImageName 是什么?
  • Images = std::map&lt;Image, sf::Texture&gt;(); 这看起来不对。 Imagesmap&lt;ImageName, Sprite&gt;,而不是 map&lt;Image, sf::Texture&gt;
  • Game::Game() : introState(imageService) 现在使用imageService 是否安全,因为它还没有被构建?
  • 我也在想也许我的 imageService 还没有构建好

标签: c++ inheritance map


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-12-17
    • 1970-01-01
    • 1970-01-01
    • 2023-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多