內const int的我有一個配置類未定義的引用shared_ptr的
// config.hpp
class Config {
public:
static constexpr int a = 1;
static constexpr int b = 1;
}
和main.cpp中包括
// main.cpp
#include "config.hpp"
int main() {
std::cout << Config::a << std::endl; // this is ok
std::shared_ptr<otherClass> stream = std::make_shared<otherClass>(
Config::a); // compile error
}
和編譯器說,undefined reference to Config::a
,並使用cout
工作時,但在shared_ptr
構造函數中不起作用。
我不知道爲什麼會發生這種情況。
你需要像一個靜態成員定義了''在命名空間範圍之前,C++ 17,即'constexpr INT配置::一個;' –
爲什麼' cout'的作品? –
這是完美轉發和odr-usage的不幸後果,使得'make_shared'無法正常工作。 'make_shared(int(Config :: a))'也可以工作 –