C++ 错误:需要类型说明符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21100391/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 23:28:52  来源:igfitidea点击:

C++ Error: Expected a type specifier

c++

提问by Xenoprimate

When I try to use a LoggerStreamlike this, I get 'expected a type specifier':

当我尝试使用这样的 a 时LoggerStream,我得到“期望一个类型说明符”:

const LoggerStream logger(L"Test Component");

const LoggerStream logger(L"Test Component");

Here's where I'm trying to use the LoggerStream:

这是我尝试使用的地方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() {

    }
}

回答by Roddy

You can't construct class members like this:-

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

  struct TestComponent::TestComponentImpl 
  {

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

Instead, use an initialization list in the constuctor, like this:

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

struct TestComponent::TestComponentImpl 
{
   LoggerStream logger;

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

    ...    
}

And I think you've fallen foul of the "Most Vexing Parse"because the compiler thinks loggermust be a function, and it's complaining that L"Test Component"is not a type specifier for a parameter.

而且我认为您已经违反了“最烦人的解析”,因为编译器认为logger必须是一个函数,并且它抱怨它L"Test Component"不是参数的类型说明符。

回答by John Kugelman

Do you mention the namespace anywhere? You need to write either:

你有没有在任何地方提到命名空间?你需要写:

using ophRuntime::LoggerStream;
const LoggerStream logger(L"Test Component");

or

或者

using namespace ophRuntime;
const LoggerStream logger(L"Test Component");

or

或者

const ophRuntime::LoggerStream logger(L"Test Component");

回答by Sebastian Hoffmann

LoggerStreamis in namespace ophRuntimethus your definition has to be

LoggerStream在命名空间中,ophRuntime因此您的定义必须是

const ophRuntime::LoggerStream logger(L"Test Component");

回答by Alex Antonov

You need to use the namespace name ophRuntimewhen use the class LoggerStream:

ophRuntime使用类时需要使用命名空间名称LoggerStream

const ophRuntime::LoggerStream logger(L"Test Component");