3

为什么我不能在运行时使用像接口这样的抽象类。

我得到输出:

1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\xmemory0(615): error C2259: 'Creature' : cannot instantiate abstract class
1>          due to following members:
1>          'std::string Creature::Move(std::vector<std::string,std::allocator<_Ty>> &)' : is abstract
1>          with
1>          [
1>              _Ty=std::string
1>          ]

1>          visual studio 2013\projects\cpp_demo\cpp_demo\creature.h(9) : see declaration of 'Creature::Move'
1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\xmemory0(614) : while compiling class template member function 'void std::allocator<_Ty>::construct(_Ty *,const _Ty &)'
1>          with
1>          [
1>              _Ty=Creature
1>          ]

1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\xmemory0(752) : see reference to function template instantiation 'void std::allocator<_Ty>::construct(_Ty *,const _Ty &)' being compiled
1>          with
1>          [
1>              _Ty=Creature
1>          ]

1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\type_traits(580) : see reference to class template instantiation 'std::allocator<_Ty>' being compiled
1>          with
1>          [
1>              _Ty=Creature
1>          ]

1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\vector(650) : see reference to class template instantiation 'std::is_empty<_Alloc>' being compiled
1>          with
1>          [
1>              _Alloc=std::allocator<Creature>
1>          ]

1>          visual studio 2013\projects\cpp_demo\cpp_demo\main.cpp(7) : see reference to class template instantiation 'std::vector<Creature,std::allocator<_Ty>>' being compiled
1>          with
1>          [
1>              _Ty=Creature
1>          ]

我的代码:

int main()
{
    unique_ptr<vector<Creature>> pCreatures(new vector<Creature>);

    unique_ptr<Creature> pHuman(new Human());
    pCreatures->push_back(*pHuman);
}



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

class Human : public Creature
{
public:
    virtual string Move(vector<string> &log);
};



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

class Creature : public IMove
{
public:
    virtual string Move(vector<string> &log) = 0;
    virtual string GetState(vector<string> &log);
};

请帮忙。

4

2 回答 2

6

您可以在 vector 或 unique_ptr 中使用抽象类,例如

#include <vector>
#include <memory>

using namespace std;

class Interface {
 public:
  virtual ~Interface() = 0;
};

Interface::~Interface() {}

class Implementation : public Interface {
};

int main(int argc, char** argv) {
  unique_ptr<Interface> p(new Implementation);
  vector<unique_ptr<Interface>> v;
  v.emplace_back(new Implementation);
  vector<Interface> vi;
  // This leads to compile error: vi.emplace_back();
}

此外,vector<Interface>只要您不调用任何可能调用的方法,您就可以使用new Interface. 例如,如果你只是声明了一个vector<Interface> v; 它编译的变量,但如果你push_backor emplace_backor resize,那么它会产生编译错误,因为它们会调用new Interface.

以上代码在gcc-4.6.3下测试。

于 2013-10-09T02:42:34.800 回答
1

您可以使用,但不要使用:

unique_ptr<vector<Creature>> pCreatures(new vector<Creature>);

利用

vector<unique_ptr<Creature>> pCreatures;

所以你将拥有一个由 unique_ptr 管理的 Creatures 指针向量。

至少有两种方法可以使用这个向量:

  1. 将对象直接创建到向量中:

    pCreatures.emplace_back(new Human());

  2. 将 unique_ptr 移至其中:

    unique_ptr pHuman(new Human());

    pCreatures.push_back(移动(pHuman));

下面是一个紧凑的用法:

int main()
{
  vector<unique_ptr<Creature>> pCreatures;

  pCreatures.emplace_back(new Human());

  unique_ptr<Creature> pHuman(new Human());
  pCreatures.push_back(move(pHuman));

  // example of usage
  pCreatures[0]->Move();
}
于 2013-10-10T21:25:03.210 回答