严重性 代码 说明 项目 文件 行 禁止显示状态 错误 LNK2019 无法解析的外部符号 "public: void __thiscall XXX

VS2017

有一段时间没写c++了,这次用vs2017写c++报这个错又有种熟悉的味道。

原因:c++在写模版函数时(template<class T>之类的),头文件不能与cpp文件分离。这就意味者,你头文件定义的含模版的地方必须在头文件中实现,没用模版定义的地方可以放在cpp中实现

比如这样:

class Caculator
{
private:
	int* data = NULL;
	int dataSIze = 0;
	int dataIndex = 0;
	char* operators = NULL;
	int operatorSize = 0;
	int operIndex = 0;
	long t_result = 0;
public:
	Caculator();
	Caculator(const Caculator& caculator);
	~Caculator();
	template<int N, int M>
	void init(int(&datas)[N], char(&_operators)[M]) {
		data = datas;
		dataSIze = N;
		operators = _operators;
		operatorSize = M;
	}
	long result();
	template<int N, int M>
	long result(int(&datas)[N], char(&_operators)[M]) {
		init(datas, _operators);
		return result();
	}
	long caculate(int a, char _operator, int b);
};

#endif

VSCode

不过值得一提的是,在VsCode里有模版的地方也可以不分离,即你这么写,并全部在cpp中实现也是没问题的

但相应地(就我个人情况),在main.cpp 中include的时候,就要写成#include"caculator.cpp" 而不是 #include"caculator.h",

我也不知道为什么,可能是因为vscode没有进行相应的配置,你include头文件他似乎找不到对应的cpp文件,从而导致报错

no refererence 空引用错误

猜你喜欢

转载自blog.csdn.net/qq_37960007/article/details/82751258
今日推荐