Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Error: Expected a type specifier

Tags:

c++

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

const LoggerStream logger(L"Test Component");

Here's where I'm trying to use the 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() {

    }
}
like image 788
Xenoprimate Avatar asked Jan 13 '14 20:01

Xenoprimate


2 Answers

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 logger must be a function, and it's complaining that L"Test Component" is not a type specifier for a parameter.

like image 82
Roddy Avatar answered Sep 17 '22 16:09

Roddy


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");
like image 29
John Kugelman Avatar answered Sep 21 '22 16:09

John Kugelman