热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

如何将元素插入到唯一指针的多维向量中?

如何解决《如何将元素插入到唯一指针的多维向量中?》经验,请问有没有懂的朋友?

我有一个带有_rounds私有成员的Turn类._rounds是另一个名为Animation的类的std唯一指针的二维std向量:

Turn.h

std::vector>> _rounds;   

Animation.h

class Animation
{
public:
    enum Type
    {
        MOVE,
        ATTACK,
        DIE,
        FADEOUT,
        MAX_TYPES
    };

//Constructors
Animation();        
Animation(Creature* creature, Animation::Type type, GameManager* gameManager, const std::function callback = nullptr);

//Getters   
const int& getOriginRowClipsIndex() { return _originRowClipsIndex; }
bool& getFinished() { return _finished; }
Type& getType() { return _type; }
Creature& getCreature() { return *_creature; }

//Setters       
void setOriginRowClipsIndex(int originRowClipsIndex) { _originRowClipsIndex = originRowClipsIndex; }

void animate(); 
void reset();
SDL_Rect* getClip(int index) {      
    return &_clips[index];      
}

private:
    GameManager* _gameManager;
    Creature * _creature;
    bool _finished;
    unsigned int _clipIndex;
    int _frequency;
    int _originRowClipsIndex; //Origin index of anim clips.     
    std::vector _clips;   
    Type _type; 
    std::function _callback;
};
#endif

我从我的代码的各个点动态分配动画,并尝试使用名为addAnimation的Turn公共方法将它们添加到Turn _rounds,如下所示:

auto animation = std::make_unique(this, Animation::Type::MOVE, _gameManager);            
turn.addAnimation(std::move(animation)); //Use move to make addAnimation take ownership of the animation.

然后,Turn addAnimation()方法尝试将Animations添加到其成员_rounds,如下所示:

Turn.cpp

void Turn::addAnimation(std::unique_ptr animation)
{   
unsigned int roundIndex;

//Set animating to true when the first animation is added to the first animation round. TODO: Move this elsewhere.
if (_rounds.size() == 0)
    _animating = true;

if (animation->getType() == Animation::MOVE) { //All move animations go on first round.
    roundIndex = 0;
    if (animation->getCreature().isPlayer()) //Set _playerMoves to be able to set monster attacks on the correct round index.
    {
        _playerMoves = true;
    }
}else if (animation->getType() == Animation::ATTACK) 
{
    roundIndex = _playerMoves ? _nAttacks + 1 : _nAttacks;
    _nAttacks++; //Increment number of attacks.     
}
else
{
    roundIndex = _rounds.size();
}

//Check if the wanted round index already exists and create if not.
if (roundIndex >= _rounds.size()) {
    //_rounds.push_back({});
    _rounds.resize(_rounds.size() + 1);
}

//Add animation to animations vector.
_rounds[roundIndex].push_back(std::move(animation));
}

但是我收到一个错误,说我正在尝试引用已删除的函数,就像我尝试使用已删除的复制构造函数一样.

> 1>------ Build started: Project: Roguelike, Configuration: Debug Win32 ------
1>  Turn.cpp
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\xmemory0(637): error C2280: 'std::unique_ptr>::unique_ptr(const std::unique_ptr<_Ty,std::default_delete<_Ty>> &)': attempting to reference a deleted function
1>          with
1>          [
1>              _Ty=Animation
1>          ]
1>  c:\program files (x86)\microsoft visual studio 14.0\vc\include\memory(1435): note: see declaration of 'std::unique_ptr>::unique_ptr'
1>          with
1>          [
1>              _Ty=Animation
1>          ]
1>  c:\program files (x86)\microsoft visual studio 14.0\vc\include\xmemory0(755): note: see reference to function template instantiation 'void std::allocator<_Ty>::construct<_Objty,std::unique_ptr>&>(_Objty *,std::unique_ptr> &)' being compiled
1>          with
1>          [
1>              _Ty=std::unique_ptr>,
1>              _Objty=std::unique_ptr>
1>          ]
1>  c:\program files (x86)\microsoft visual studio 14.0\vc\include\xmemory0(755): note: see reference to function template instantiation 'void std::allocator<_Ty>::construct<_Objty,std::unique_ptr>&>(_Objty *,std::unique_ptr> &)' being compiled
1>          with
1>          [
1>              _Ty=std::unique_ptr>,
1>              _Objty=std::unique_ptr>
1>          ]
1>  c:\program files (x86)\microsoft visual studio 14.0\vc\include\xmemory0(894): note: see reference to function template instantiation 'void std::allocator_traits<_Alloc>::construct<_Ty,std::unique_ptr>&>(std::allocator<_Ty> &,_Objty *,std::unique_ptr> &)' being compiled
1>          with
1>          [
1>              _Alloc=std::allocator>>,
1>              _Ty=std::unique_ptr>,
1>              _Objty=std::unique_ptr>
1>          ]
1>  c:\program files (x86)\microsoft visual studio 14.0\vc\include\xmemory0(893): note: see reference to function template instantiation 'void std::allocator_traits<_Alloc>::construct<_Ty,std::unique_ptr>&>(std::allocator<_Ty> &,_Objty *,std::unique_ptr> &)' being compiled
1>          with
1>          [
1>              _Alloc=std::allocator>>,
1>              _Ty=std::unique_ptr>,
1>              _Objty=std::unique_ptr>
1>          ]
1>  c:\program files (x86)\microsoft visual studio 14.0\vc\include\vector(1286): note: see reference to function template instantiation 'void std::_Wrap_alloc>::construct>,std::unique_ptr>&>(_Ty *,std::unique_ptr> &)' being compiled
1>          with
1>          [
1>              _Ty=std::unique_ptr>
1>          ]
1>  c:\program files (x86)\microsoft visual studio 14.0\vc\include\vector(1285): note: see reference to function template instantiation 'void std::_Wrap_alloc>::construct>,std::unique_ptr>&>(_Ty *,std::unique_ptr> &)' being compiled
1>          with
1>          [
1>              _Ty=std::unique_ptr>
1>          ]
1>  c:\program files (x86)\microsoft visual studio 14.0\vc\include\vector(1278): note: while compiling class template member function 'void std::vector>,std::allocator>>>::push_back(const std::unique_ptr<_Ty,std::default_delete<_Ty>> &)'
1>          with
1>          [
1>              _Ty=Animation
1>          ]
1>  c:\cpp\roguelike\roguelike\turn.cpp(44): note: see reference to function template instantiation 'void std::vector>,std::allocator>>>::push_back(const std::unique_ptr<_Ty,std::default_delete<_Ty>> &)' being compiled
1>          with
1>          [
1>              _Ty=Animation
1>          ]
1>  c:\cpp\roguelike\roguelike\turn.cpp(44): note: see reference to class template instantiation 'std::vector>,std::allocator>>>' being compiled
1>          with
1>          [
1>              _Ty=Animation
1>          ]
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

我的代码出现在模板错误的长链末尾的唯一一点是push_back到addAnimation的唯一指针向量.

如果我尝试编译一个简化的案例,我甚至可以向Animation类添加自定义构造函数:

#include "stdafx.h"
#include 
#include 
#include 

class Animation
{
    int _animation_value;
public:
    Animation() {};
    Animation(int animation_value)
        : _animation_value(animation_value)
    {}
};

class Turn
{
    std::vector>> _rounds;
public:
    void addAnimation(std::unique_ptr round)
    {
        _rounds.resize(_rounds.size() + 1);
        _rounds[0].push_back(std::move(round));
    }
};

class Other
{
public:
    void foo()
    {
        auto x = std::make_unique(7);
        Turn turn;
        turn.addAnimation(std::move(x));
    }
};

int main()
{
    Other other;
    other.foo();        
    return 0;
}

有帮助吗?


推荐阅读
  • 本文记录了作者对x265开源代码的实现与框架进行学习与探索的过程,包括x265的下载地址与参考资料,以及在Win7 32 bit PC、VS2010平台上的安装与配置步骤。 ... [详细]
  • 本文介绍了brain的意思、读音、翻译、用法、发音、词组、同反义词等内容,以及脑新东方在线英语词典的相关信息。还包括了brain的词汇搭配、形容词和名词的用法,以及与brain相关的短语和词组。此外,还介绍了与brain相关的医学术语和智囊团等相关内容。 ... [详细]
  • 本文介绍了[从头学数学]中第101节关于比例的相关问题的研究和修炼过程。主要内容包括[机器小伟]和[工程师阿伟]一起研究比例的相关问题,并给出了一个求比例的函数scale的实现。 ... [详细]
  • C语言注释工具及快捷键,删除C语言注释工具的实现思路
    本文介绍了C语言中注释的两种方式以及注释的作用,提供了删除C语言注释的工具实现思路,并分享了C语言中注释的快捷键操作方法。 ... [详细]
  • 展开全部下面的代码是创建一个立方体Thisexamplescreatesanddisplaysasimplebox.#Thefirstlineloadstheinit_disp ... [详细]
  • 不同优化算法的比较分析及实验验证
    本文介绍了神经网络优化中常用的优化方法,包括学习率调整和梯度估计修正,并通过实验验证了不同优化算法的效果。实验结果表明,Adam算法在综合考虑学习率调整和梯度估计修正方面表现较好。该研究对于优化神经网络的训练过程具有指导意义。 ... [详细]
  • 本文介绍了作者在开发过程中遇到的问题,即播放框架内容安全策略设置不起作用的错误。作者通过使用编译时依赖注入的方式解决了这个问题,并分享了解决方案。文章详细描述了问题的出现情况、错误输出内容以及解决方案的具体步骤。如果你也遇到了类似的问题,本文可能对你有一定的参考价值。 ... [详细]
  • 利用Visual Basic开发SAP接口程序初探的方法与原理
    本文介绍了利用Visual Basic开发SAP接口程序的方法与原理,以及SAP R/3系统的特点和二次开发平台ABAP的使用。通过程序接口自动读取SAP R/3的数据表或视图,在外部进行处理和利用水晶报表等工具生成符合中国人习惯的报表样式。具体介绍了RFC调用的原理和模型,并强调本文主要不讨论SAP R/3函数的开发,而是针对使用SAP的公司的非ABAP开发人员提供了初步的接口程序开发指导。 ... [详细]
  • HTML5网页模板怎么加百度统计?
    本文介绍了如何在HTML5网页模板中加入百度统计,并对模板文件、css样式表、js插件库等内容进行了说明。同时还解答了关于HTML5网页模板的使用方法、表单提交、域名和空间的问题,并介绍了如何使用Visual Studio 2010创建HTML5模板。此外,还提到了使用Jquery编写美好的HTML5前端框架模板的方法,以及制作企业HTML5网站模板和支持HTML5的CMS。 ... [详细]
  • Carve库在Visual Studio2015中的编译方法及注意事项
    本文介绍了在Visual Studio2015中编译Carve库的方法及注意事项。首先下载Carve库,并使用Visual Studio2015打开,生成后在bin目录下会生成.lib文件。同时,本文还指出了之前在Visual Studio2017中编译的问题,并提醒需要根据对应的平台进行编译,否则会出现报错。详细的步骤和注意事项请参考原文链接。 ... [详细]
  • 本文概述了JNI的原理以及常用方法。JNI提供了一种Java字节码调用C/C++的解决方案,但引用类型不能直接在Native层使用,需要进行类型转化。多维数组(包括二维数组)都是引用类型,需要使用jobjectArray类型来存取其值。此外,由于Java支持函数重载,根据函数名无法找到对应的JNI函数,因此介绍了JNI函数签名信息的解决方案。 ... [详细]
  • linux进阶50——无锁CAS
    1.概念比较并交换(compareandswap,CAS),是原⼦操作的⼀种,可⽤于在多线程编程中实现不被打断的数据交换操作࿰ ... [详细]
  • 通过Anaconda安装tensorflow,并安装运行spyder编译器的完整教程
    本文提供了一个完整的教程,介绍了如何通过Anaconda安装tensorflow,并安装运行spyder编译器。文章详细介绍了安装Anaconda、创建tensorflow环境、安装GPU版本tensorflow、安装和运行Spyder编译器以及安装OpenCV等步骤。该教程适用于Windows 8操作系统,并提供了相关的网址供参考。通过本教程,读者可以轻松地安装和配置tensorflow环境,以及运行spyder编译器进行开发。 ... [详细]
  • loader资源模块加载器webpack资源模块加载webpack内部(内部loader)默认只会处理javascript文件,也就是说它会把打包过程中所有遇到的 ... [详细]
  • 工作经验谈之-让百度地图API调用数据库内容 及详解
    这段时间,所在项目中要用到的一个模块,就是让数据库中的内容在百度地图上展现出来,如经纬度。主要实现以下几点功能:1.读取数据库中的经纬度值在百度上标注出来。2.点击标注弹出对应信息。3 ... [详细]
author-avatar
yuliu预留
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有