C++ 错误:“test”的外部定义与“B<dim>”中的任何声明都不匹配
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22579666/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
error: out-of-line definition of 'test' does not match any declaration in 'B<dim>'
提问by Fahad Alrashed
I have a small problem that is killing me!! I don't know what seems to be wrong with the below code. I should be able to implement the function that is inherited from the super class, shouldn't I? but I get error: out-of-line definition of 'test' does not match any declaration in 'B<dim>'
我有一个小问题正在杀死我!!我不知道下面的代码似乎有什么问题。我应该能够实现从超类继承的函数,不是吗?但我明白了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;
}
I am on a Mac using clang (Apple LLVM version 5.1).
我在 Mac 上使用 clang(Apple LLVM 5.1 版)。
采纳答案by π?ντα ?ε?
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;
}
You still need to definethe function declared the class declaration.
您仍然需要在类声明中定义函数声明。
回答by Vlad from Moscow
The problem is that you are trying to define function test outside the class definition of class B. You have to declare it at first in the class
问题是你试图在类 B 的类定义之外定义函数 test。你必须首先在类中声明它
template <int dim>
class B : public A <dim>
{
double test() const;
};