Can't initialize static smart pointer

I have a class with a member being a static smart pointer to an object of a different class. Whenever I try to initialize the smart pointer, I always get an error saying "deceleration is incompatible":

1
2
3
4
5
6
7
8
9
10
11
12
//SomeClass.h
#include <iostream>
#include <memory>
#include "OtherClass.h"

class SomeClass
{
protected:
     static std::unique_ptr<OtherClass> foo;
public:
     SomeClass();
}


1
2
3
4
//SomeClass.cpp
#include "SomeClass.h"

std::unique_ptr<OtherClass> SomeClass::foo = new OtherClass;


What am I doing wrong, and is there any way to fix this?
You need direct initialization:
std::unique_ptr<OtherClass> SomeClass::foo(new OtherClass);

Thanks for the help. Why exactly do static smart pointers need to be directly initialized, rather than being initialized with the = operator?
Because unique_ptr constructor, which takes pointer, is explicit and cannot be used in this context (and also in implicit conversion context).

Alternative would be:
1
2
3
std::unique_ptr<OtherClass> SomeClass::foo = static_cast<std::unique_ptr<OtherClass>>(new OtherClass);
//or (preferable even over my first post, but requires C++14)
std::unique_ptr<OtherClass> SomeClass::foo = std::make_unique<OtherClass>();
Topic archived. No new replies allowed.