小编Pat*_*ouf的帖子

取消定义对...的引用:从模板类继承

我在从模板类继承时遇到一些问题。下面的代码无法编译,显示此错误:main.cpp : undefined reference to OBJ1<1000>::method()

父级.h

template <int nb>
class PARENT
{
  PARENT() {};
  ~PARENT() {};

  virtual void method() = 0;
  enum { nb_ = nb };
};
Run Code Online (Sandbox Code Playgroud)

对象1.h

#include "parent.h"

template <int nb>
class OBJ1 : public PARENT<nb>
{
  virtual void method();
};
Run Code Online (Sandbox Code Playgroud)

对象1.cpp

#include "obj1.h"

template <int nb>
void OBJ1<nb>::method()
{
  //code
}
Run Code Online (Sandbox Code Playgroud)

主程序

#include "obj1.h"

int main()
{
  OBJ1<1000> toto;
  toto.method();
}
Run Code Online (Sandbox Code Playgroud)

我哪里错了?

c++ inheritance templates

1
推荐指数
1
解决办法
1040
查看次数

标签 统计

c++ ×1

inheritance ×1

templates ×1