Fah*_*hed 9 c++ macos virtual inheritance templates
我有一个小问题就是杀了我!! 我不知道下面的代码似乎有什么问题.我应该能够实现从超类继承的函数,不应该吗?但我明白了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;
}
Run Code Online (Sandbox Code Playgroud)
我在使用clang(Apple LLVM 5.1版)的Mac上.
πάν*_*ῥεῖ 11
尝试
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;
}
Run Code Online (Sandbox Code Playgroud)
您仍然需要定义声明类声明的函数.
问题是你试图在类 B 的类定义之外定义函数测试。你必须首先在类中声明它
template <int dim>
class B : public A <dim>
{
double test() const;
};
Run Code Online (Sandbox Code Playgroud)