#include <iostream>
#include <vector>
#include <memory>
class Node{
public:
static constexpr int data_size = sizeof(int);
};
class View{
public:
View(int size){
}
};
class Header: public Node{
public:
void foo(){
std::shared_ptr<View> v = std::make_shared<View>(data_size);
}
void bar(){
std::shared_ptr<View> v(new View(data_size));
}
View bar1(){
return View(data_size);
}
void bar2(){
View *v = new View(data_size);
}
int bar3(){
return data_size;
}
};
int main() {
Header *h = new Header();
// This 1 lines below will produce the error
h->foo();
// These 4 lines are ok
h->bar();
h->bar1();
h->bar2();
h->bar3();
return 0;
}
When call foo() , error below will occur :
/Applications/CLion.app/Contents/bin/cmake/bin/cmake --build /Users/everettjf/code/cpptest/cmake-build-debug --target all -- -j 8
Scanning dependencies of target cpptest
[ 50%] Building CXX object CMakeFiles/cpptest.dir/main.cpp.o
[100%] Linking CXX executable cpptest
Undefined symbols for architecture x86_64:
"Node::data_size", referenced from:
Header::foo() in main.cpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [cpptest] Error 1
make[1]: *** [CMakeFiles/cpptest.dir/all] Error 2
make: *** [all] Error 2
When I use new to initialize the shared_ptr ,it is ok.But when I use make_shared, the link error occurs.
Why std::make_shared different with new when construct with static constexpr member ?
My environment is macOS with Clion, CMakeList.txt is :
cmake_minimum_required(VERSION 3.6)
project(cpptest)
set(CMAKE_CXX_STANDARD 11)
set(SOURCE_FILES main.cpp)
add_executable(cpptest ${SOURCE_FILES})