我有单例类,编译时会出错。 这是代码:
mySingleton.h
#pragma once
class MySingleton
{
public:
static MySingleton& instance(); // Singleton
void doSomething();
private:
MySingleton() : _i(1) {}
int _i;
};
mySingleton.cpp
#include "mySingleton.h"
MySingleton& MySingleton::instance()
{
static MySingleton s;
return s;
}
void MySingleton::doSomething()
{
std::wcout << "test singleton" << _i << std::endl;
}
main.cpp
#include "mySingleton.h"
#include <conio.h>
using namespace std;
int main(int argc, char* argv[])
{
MySingleton::instance().doSomething();
getch();
return 0;
}
CMakeLists.txt
cmake_minimum_required(VERSION 3.7)
project(test)
add_executable(test main.cpp)
错误:
严重性代码描述项目文件行抑制状态 错误LNK2019无法解析的外部符号“ public:静态类 MySingleton和__cdecl MySingleton :: instance(void)” 函数中引用的(?instance @ MySingleton @@ SAAEAV1 @ XZ) 主C:\ Program Files(x86)\ Microsoft Visual Studio \ 2017 \ Professional \ Common7 \ IDE \ CMakeLists.txt C:\ Program文件 (x86)\ Microsoft Visual Studio \ 2017 \ Professional \ Common7 \ IDE \ main.cpp.obj 1
严重性代码描述项目文件行抑制状态 错误LNK2019无法解析的外部符号“ public:void __cdecl MySingleton :: doSomething(void)”(?doSomething @ MySingleton @@ QEAAXXZ) 在函数主C:\ Program Files(x86)\ Microsoft Visual中引用 Studio \ 2017 \ Professional \ Common7 \ IDE \ CMakeLists.txt C:\ Program文件 (x86)\ Microsoft Visual Studio \ 2017 \ Professional \ Common7 \ IDE \ main.cpp.obj 1
出什么问题了?