C++ 错误 : Expected a type specifier

标签 c++

当我尝试像这样使用 LoggerStream 时,我得到“需要一个类型说明符”:

const LoggerStream logger(L"测试组件");

这是我尝试使用 LoggerStream 的地方:

#include "Logger.h"
#include "TestComponent.h"

namespace ophRuntime {
    struct TestComponent::TestComponentImpl {


        private:
        LoggerStream logger(L"Test Component");

        NO_COPY_OR_ASSIGN(TestComponentImpl);
    };

    TestComponent::TestComponent() : impl(new TestComponentImpl()) {

    }

    bool TestComponent::Init() {

    }
}

最佳答案

你不能像这样构造类成员:-

  struct TestComponent::TestComponentImpl 
  {

  private:
        LoggerStream logger(L"Test Component");

相反,在构造函数中使用初始化列表,如下所示:

struct TestComponent::TestComponentImpl 
{
   LoggerStream logger;

    TestComponent::TestComponent() : impl(new TestComponentImpl()),
                                     logger("L"Test Component")
    {   
    }

    ...    
}

而且我认为您违反了 "Most Vexing Parse"因为编译器认为 logger 必须是一个函数,并且它提示 L"Test Component" 不是参数的类型说明符。

关于C++ 错误 : Expected a type specifier,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21100391/

相关文章:

c++ - 默认的构造函数和析构函数是内联的吗?

c++ - 类模板,在定义中引用它自己的类型

c++ - 如何 ftp 正在使用的文件?

c++ - 如何用 C++03 中的成员函数返回的值填充 vector ?

c++ - 一个简单的 C++11 宏

c++ - PJSUA2 - 在 Windows 中使用 Null-Audio 将调用录制到 WAV

c++ - QT:来自多个旋转框的项目列表

c++ - 为什么我不能将此对象推送到我的 std::list 中?

c++ - Boost.Iostreams 与 iostream/streambuf 重载比特流 I/O

c++ - moveToThread() 而不是 start() 适用于未构造的对象?