无法使用c ++ 11实例化抽象类

时间:2013-10-09 01:53:55

标签: c++ c++11

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

我得到了输出:

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);
};

请帮忙。

2 个答案:

答案 0 :(得分:4)

您可以在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_backemplace_backresize,则会出现编译错误,因为它们会调用{{ 1}}。

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

答案 1 :(得分:0)

您可以使用,而不是使用:

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

使用

vector<unique_ptr<Creature>> pCreatures;

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

至少有两种使用此向量的方法:

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

    pCreatures.emplace_back(new Human());

  2. 将unique_ptr移动到它:

    unique_ptr pHuman(new Human());

    pCreatures.push_back(移动(pHuman));

  3. 以下是紧凑的用法:

    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();
    }