简体   繁体   English

尝试将用户定义的Vector类型插入用户定义的Vectors中

[英]Trying to insert User defined type of Vector into User defined type Vectors

I'm trying to make a vector of vectors in a spell system for a small RPG battle simulation. 我正在尝试在小型RPG战斗模拟的咒语系统中创建向量的向量。 The main goal I'm trying to accomplish is have a vector of Spells that the player can access and select what type of Spells they want to cast: Fire, Ice, etc. and then choose the name of the spell: burn, frost, etc. 我要实现的主要目标是拥有一个向量咒语,玩家可以访问并选择他们想施放的咒语类型:火,冰等,然后选择咒语的名称:燃烧,冰霜,等等

The code below is what I've declared Spell and FireSpell as. 下面的代码是我将Spell和FireSpell声明为的代码。 I'm trying to insert a vector of FireSpells into a vector of type vector spells, however, it does not insert. 我正在尝试将FireSpells的向量插入类型为vector的向量的向量中,但是不会插入。 Is there a way to make it insert? 有没有办法使其插入?

I've tried the push_back on a FireSpell into a vector of Spells and it works fine, however, it always returns an error when I push_back a FireSpell vector into a vector of type vector Spell. 我已经将FireSpell上的push_back尝试到Spells的向量中,并且工作正常,但是,当我将FireSpell向量推到vector Spell类型的向量中时,它总是返回错误。

Magic.h Magic.h

struct Fire
{
    int damage;
    int dps;
}; 

struct Spell
{
    int cost;
    string name;
};

struct FireSpell : Spell, Fire
{

};

Magic.cpp Magic.cpp

#include <iostream>
#include "MagicSys.h"
#include <vector>
#include <string>
#include "Magic.h"

using std::cout;
using std::vector;

FireSpell burn;
FireSpell inferno;
FireSpell volcano;


int mainfunc()
{
    vector<vector<Spell>> PlayerSpells;
    vector<FireSpell> PlayerFSpells;
    PlayerFSpells.push_back(burn);
    PlayerFSpells.push_back(inferno);
    PlayerFSpells.push_back(volcano);

 /*Trying to insert FireSpell Vector into the end of the vector of Spell vectors*/

    PlayerSpells.push_back(PlayerFSpells);

    vector<vector<Spell>>::iterator it;
    for (it = PlayerSpells.begin(); it!=PlayerSpells.end(); ++it)
        cout << &it << " ";

    return 0;
}

I expected the inner FireSpell vector to insert into the outer Spell vector on the line: PlayerSpells.push_back(PlayerFSpells); 我期望内部的FireSpell向量插入到该行的外部Spell向量中: PlayerSpells.push_back(PlayerFSpells); , however, it gives me the error message: ,但是,它给了我错误信息:

no instance of overloaded function "std::vector<_Ty, _Alloc>::push_back
[with _Ty=std::vector<Spell, std::allocator<Spell>>,
_Alloc=std::allocator<std::vector<Spell, std::allocator<Spell>>>]"
matches the argument list

If you come from some other languages, you may be used to treat mutable values as implicit references. 如果您来自其他语言,则可能会将可变值视为隐式引用。 In those languages, you typically gain the ability to easily construct collections of different types, which may or may not share an interface, and you may or may not treat them polymorphically (late binding). 使用这些语言,通常可以轻松构造不同类型的集合,这些集合可以共享或可以不共享接口,并且可以或可以不对它们进行多态处理(后期绑定)。

In C++, however, objects are not references: they are the actual objects in memory, which requires them to have a compile-time-known size. 但是,在C ++中,对象不是引用:它们是内存中的实际对象,这要求它们具有编译时已知的大小。 You can still rather easily create polymorphic collections through a pointer or a reference, though, or even have collections of objects without a common interface; 但是,您仍然可以通过指针或引用轻松地创建多态集合,甚至可以包含没有公共接口的对象集合。 but you have to be explicit about it -- and know the rules of the language to prevent gotchas like slicing (see What is object slicing? for instance). 但您必须对此加以明确-并了解语言规则,以防止出现诸如切片之类的陷阱(例如,请参阅什么是对象切片? )。

Having

 vector<vector<Spell>> PlayerSpells; vector<FireSpell> PlayerFSpells; 

you cannot do 你做不到

 PlayerSpells.push_back(PlayerFSpells); 

because a vector<FireSpell> is not a vector<Spell> 因为vector<FireSpell>不是vector<Spell>

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

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