错误:'test'的外联定义与'B<dim>'中的任何声明都不匹配

error: out-of-line definition of 'test' does not match any declaration in 'B<dim>'

本文关键字:gt dim 任何 不匹配 lt 声明 定义 test 错误      更新时间:2023-10-16

我有个小问题快把我折磨死了!我不知道下面的代码似乎有什么问题。我应该能够实现从超类继承的功能,不是吗?但我得到error: out-of-line definition of 'test' does not match any declaration in 'B<dim>'

template <int dim>
class A 
{
public:
  virtual double test() const ;
};
template <int dim>
class B : public A <dim>
{
};
template <int dim>
double B<dim>::test () const
{
  return 0;
}

我在Mac上使用clang (Apple LLVM version 5.1)。

Try

template <int dim>
class B : public A <dim>
{
public:
     virtual double test () const;
};
// Function definition
template <int dim>
double B<dim>::test () const
{
  return 0;
}

您仍然需要定义类声明中声明的函数

问题是您试图在类b的类定义之外定义功能测试。您必须首先在类

中声明它
template <int dim>
class B : public A <dim>
{
   double test() const;
};