error::make_unique is not a member of ‘std’
make_unique
is an upcoming C++14 feature and thus might not be available on your compiler, even if it is C++11 compliant.
You can however easily roll your own implementation:
template<typename T, typename... Args>
std::unique_ptr<T> make_unique(Args&&... args) {
return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
}
(FYI, here is the final version of make_unique
that was voted into C++14. This includes additional functions to cover arrays, but the general idea is still the same.)
make_unique' is not a member of 'std'
I fixed it by replacing -std=c++17 with set(CMAKE_CXX_STANDARD 17)
Why is compiler giving error while using make_unique with array?
You need
arr = std::make_unique<int[]>(size);
make_unique
has a specific documented usage:
template< class T > unique_ptr<T> make_unique( std::size_t size );`
... Constructs an array of the given dynamic size. The array elements are value-initialized. This overload participates in overload resolution only if T is an array of unknown bound. The function is equivalent to:
unique_ptr<T>(new std::remove_extent_t<T>[size]())
Conceptually-speaking1, you can think of make_unique as being very similar to std::unique_ptr<T>(new T(...))
where ... represents forwarding of the args passed to make_unique. In this case, the usage is analogous to forwarding the one arg to operator new[] (std::size_t size)
1 The documentation I linked above doesn't say that the arg is forwarded in the array case, and I don't believe the spec does either.
no member named 'make_unique' in namespace 'std'
It seems I resolved the issue. I needed to install libc++-dev
and make sure -stdlib=libc++
is in the command line.
Trouble with unique_ptr : not a member of 'std'
CFLAGS
is for C compilers. You are using C++ and a C++ compiler. Use CXXFLAGS
in your Makefile to set C++ compiler's flags:
NAME = plazza
G++ = g++
CXXFLAGS = -W -Wall -Wextra -Werror -std=c++11
SRC = main.cpp
Since you are setting C flags, C++11 is not enabled because -std=c++11
is not passed to your C++ compiler. If you compiled with a C compiler, the compiler (at least GCC does it AFAIK) would warn about the C++ flag being set on the C compiler. You could use make VERBOSE=1
in these kind of compiler error situations for debugging.
Why is std::make_unique necessary to initialize member std::unique array, and how to work around for C++11?
1. The constructor unique_ptr( pointer p )
is marked explicit
, which prevents instances of unique_ptr
from being constructed implicitly in array list initialization.
2. std::make_unique
is available since C++14.
In C++11 a possible solution to both points is to construct an instance explicitly e.g. std::unique_ptr<Bar>(new Bar( 42, 43 ))
.
Trouble using std::make_unique with member variable of class
Your issue is nothing to do with the class member, rather its type!
std::make_unique()
returns std::unique_ptr
for the template type T
(i.e. std::unique_ptr
of an instance of type T
)
template< class T, class... Args >
unique_ptr<T> make_unique( Args&&... args );
^^^^^^^^^^^^^^
The member
CChristianLifeMinistryHtmlView *m_pHtmlPreview;
is a pointer to a CChristianLifeMinistryHtmlView
, not a std::unique_ptr
. Hence, the type mismatch.
How do I use
make_unique
with a member variable of the class?
Therefore, you need to use std::unique_ptr<CChristianLifeMinistryHtmlView>
as the type of the m_pHtmlPreview
member:
std::unique_ptr<CChristianLifeMinistryHtmlView> m_pHtmlPreview;
...
m_pHtmlPreview = std::make_unique<CChristianLifeMinistryHtmlView>();
If it is a long typing, a type alias wouldn't be a bad idea:
using UniqueCLMHView = std::unique_ptr<CChristianLifeMinistryHtmlView>;
UniqueCLMHView m_pHtmlPreview;
...
m_pHtmlPreview = std::make_unique<CChristianLifeMinistryHtmlView>();
error: ‘unique_ptr’ is not a member of ‘std’
You need to include header where unique_ptr
and shared_ptr
are defined
#include <memory>
As you already knew that you need to compile with c++11
flag
g++ main.cpp -o run -std=c++11
// ^
Related Topics
Does an R Compiler to C/C++ Exist
Is Returning References of Member Variables Bad Practice
Boost-Python How to Pass a C++ Class Instance to a Python Class
Lnk2019: Unresolved External Symbol _Main Referenced in Function _Tmaincrtstartup
Implicit Conversion When Overloading Operators for Template Classes
Error C2065: 'Cout':Undeclared Identifier
Handling Overflow When Casting Doubles to Integers in C
Was Not Declared in This Scope' Error
Why Does Constexpr Static Member (Of Type Class) Require a Definition
How Do Memory_Order_Seq_Cst and Memory_Order_Acq_Rel Differ
Project Error: Unknown Module(S) in Qt: Webkitwidgets
How to Return Array from C++ Function to Python Using Ctypes
How to Parallelize a for Loop Through a C++ Std::List Using Openmp