error: expected unqualified-id before ‘const’

error: expected unqualified-id before ‘const’

我看到了一些关于这个错误的问题,但我没有太多用 C++ 制作 class 的经验,所以我实际上不明白答案的意思。我还应该指出,我没有写这段代码。

我收到标题中所述的错误,我相信它来自这个 header 文件,但我不知道错误的含义以及如何修复它。

这是文件:

#ifndef _QUICKTIMER_H_
#define _QUICKTIMER_H_

#include <cstdlib>
#include <string>
#include <chrono>

class QuickTimer {
public:
  QuickTimer(const std::string& prefix = "");
  ~QuickTimer();
private:
  std::chrono::high_resolution_clock::time_point mStartTime;
  const std::string mPrefix;
};

#endif

以及完整的错误:

error: expected unqualified-id before ‘const’
 QuickTimer(const std::string& prefix) :
            ^  

error: expected ‘)’ before ‘const’

error: declaration of ‘~QuickTimer’ as non-member
 ~QuickTimer()
             ^

如果有人能向我解释这是什么意思以及发生了什么,我将不胜感激,谢谢!

Class 名称前缀可能在您的构造函数和析构函数的定义中丢失。你应该在 cpp 文件中有类似的东西:

QuickTimer::QuickTimer(const std::string& prefix)
{
}

QuickTimer::~QuickTimer()
{
}