解决方法
- 将单例对象初始化放入.cpp文件
示例
Fun.h
#ifdef FUN_EXPORTS
#define FUN_API __declspec(dllexport)
#else
#define FUN_API __declspec(dllimport)
#endif
#include <iostream>
class FUN_API Fun {
public:
static Fun& instance();
void showThis() {
std::cout << this << std::endl;
}
};
Fun.cpp
#include "pch.h"
#include "Fun.h"
Fun& Fun::instance()
{
static Fun fun;
return fun;
}
测试
#include <iostream>
#include "Fun.h"
int main()
{
Fun::instance().showThis();
std::cout << &Fun::instance() << std::endl;
std::cout << "End\n";
}
结果