2

我有一个 C++ 类头文件,它内联定义了许多函数。我想将这些函数移到头文件之外并放入一个单独的.cpp文件中以加快编译速度。虽然我可以将普通函数移动到一个单独的文件中,并且只在标题中保留函数减速,但是当我尝试将虚拟函数移动到时,.cpp我收到以下错误:

错误 2 - 错误 C2723:函数定义上的“虚拟”存储类说明符非法

我怎么做?功能如下:

 virtual void SoundMixerSub::SetFilters(const MixerFilter& f)
 { 
....
 }
4

1 回答 1

11

As it says, you can't have virtual on the function definition outside the class, as per §7.1.2:

The virtual specifier shall be used only in the initial declaration of a non-static class member function

Keep the virtual on the declaration and remove it from the definition. So in the header file:

class SoundMixerSub : ...
{
  // ...
  virtual void SetFilters(const MixerFilter&);
  // ...
};

Then in the implementation file:

void SoundMixerSub::SetFilters(const MixerFilter& f)
{ 
  // ...
}
于 2013-03-24T14:52:52.210 回答