C++中使用类模板分文件编写的坑

C++中使用类模板分文件编写的坑,第1张

错误内容 开发平台:Microsoft Visual Studio 2022 错误提示:

LNK2019 无法解析的外部符号 “public: __cdecl Father::Father(int)” (??0?KaTeX parse error: Undefined control sequence: \Microsoft at position 48: …该符号 Project9 E:\̲M̲i̲c̲r̲o̲s̲o̲f̲t̲ ̲Visual Studio\c…Father@H@@QEBA?AV0@AEBV0@@Z),函数 main 中引用了该符号 Project9 E:\Microsoft Visual Studio\code\Project9\main.obj 1
错误
LNK2019 无法解析的外部符号 "public: void __cdecl Father::prfT(void)const " (?prfT@?$Father@H@@QEBAXXZ),函数 main 中引用了该符号 Project9 E:\Microsoft Visual Studio\code\Project9\main.obj 1

错误原因

C++中类模板比较特殊会二次编译,所以分文件编写时编译器找不到成员函数的实现

代码: 文件结构

Father.h
#pragma once

template<typename T>
class Father {
public:
	Father(T a);

	T getA()const;
	Father operator+(const Father& other) const;
	void prfT() const;

private:
	T a;
};
Father.cpp
#include 
#include "Father.h"

// 类成员函数需要加类模板声明
template<typename T>
Father<T>::Father<T>(T a) {  // 作用域需要加虚拟参数列表
	this->a = a;
}

template<typename T>
T Father<T>::getA()const {
	return a;
}

template<typename T>
Father<T> Father<T>::operator+(const Father<T>& other) const {
	// 返回值是类对象也需要加虚拟参数列表
	// 成员函数的参数是类对象也需要加虚拟参数列表
	
	// 成员函数类参数列表可以不加
	// 普通类型-》类类型,等同于 Father(T),调用带参构造函数
	Father temp = this->getA() + other.getA();  

	return temp;
}

template<typename T>
void Father<T>::prfT() const {
	std::cout << a << std::endl;
}
main.cpp
#include "Father.h"

int main() {
	Father<int> f1(12);
	Father<int> f2(13);

	Father<int> f3 = f1 + f2;

	f3.prfT();

	return 0;
}
解决方案: 1、把主函数放在类模板实现中,就是放在Father.cpp文件中 Father.h
#pragma once

template<typename T>
class Father {
public:
	Father(T a);

	T getA()const;
	Father operator+(const Father& other) const;
	void prfT() const;

private:
	T a;
};
Father.cpp
#include 
#include "Father.h"

// 类成员函数需要加类模板声明
template<typename T>
Father<T>::Father<T>(T a) {  // 作用域需要加虚拟参数列表
	this->a = a;
}

template<typename T>
T Father<T>::getA()const {
	return a;
}

template<typename T>
Father<T> Father<T>::operator+(const Father<T>& other) const {
	// 返回值是类对象也需要加虚拟参数列表
	// 成员函数的参数是类对象也需要加虚拟参数列表

	// 成员函数类参数列表可以不加
	// 普通类型-》类类型,等同于 Father(T),调用带参构造函数
	Father temp = this->getA() + other.getA();

	return temp;
}

template<typename T>
void Father<T>::prfT() const {
	std::cout << a << std::endl;
}


int main() {
	Father<int> f1(12);
	Father<int> f2(13);

	Father<int> f3 = f1 + f2;

	f3.prfT();

	return 0;
}
2、把类模板声明和实现放在同一个文件中,文件名使用hpp后缀 Father.hpp
#pragma once
#include 

template<typename T>
class Father {
public:
	Father(T a);

	T getA()const;
	Father operator+(const Father& other) const;
	void prfT() const;

private:
	T a;
};


// 类成员函数需要加类模板声明
template<typename T>
Father<T>::Father<T>(T a) {  // 作用域需要加虚拟参数列表
	this->a = a;
}

template<typename T>
T Father<T>::getA()const {
	return a;
}

template<typename T>
Father<T> Father<T>::operator+(const Father<T>& other) const {
	// 返回值是类对象也需要加虚拟参数列表
	// 成员函数的参数是类对象也需要加虚拟参数列表

	// 成员函数类参数列表可以不加
	// 普通类型-》类类型,等同于 Father(T),调用带参构造函数
	Father temp = this->getA() + other.getA();

	return temp;
}

template<typename T>
void Father<T>::prfT() const {
	std::cout << a << std::endl;
}
main.cpp
#include "Father.hpp"

int main() {
	Father<int> f1(12);
	Father<int> f2(13);

	Father<int> f3 = f1 + f2;

	f3.prfT();

	return 0;
}
3、类模板声明和实现还是分文件,但是实现文件后缀改成hpp,主函数文件导入hpp文件而不是.h头文件(常用) 文件结构

Father.h
#pragma once


template<typename T>
class Father {
public:
	Father(T a);

	T getA()const;
	Father operator+(const Father& other) const;
	void prfT() const;

private:
	T a;
};
Father.hpp
#include 
#include "Father.h"

// 类成员函数需要加类模板声明
template<typename T>
Father<T>::Father<T>(T a) {  // 作用域需要加虚拟参数列表
	this->a = a;
}

template<typename T>
T Father<T>::getA()const {
	return a;
}

template<typename T>
Father<T> Father<T>::operator+(const Father<T>& other) const {
	// 返回值是类对象也需要加虚拟参数列表
	// 成员函数的参数是类对象也需要加虚拟参数列表
	Father temp = this->getA() + other.getA();  // 成员函数类参数列表可以不加

	return temp;
}

template<typename T>
void Father<T>::prfT() const {
	std::cout << a << std::endl;
}
main.cpp
#include "Father.hpp"

int main() {
	Father<int> f1(12);
	Father<int> f2(13);

	Father<int> f3 = f1 + f2;

	f3.prfT();

	return 0;
}

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1325543.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-12
下一篇 2022-06-12

发表评论

登录后才能评论

评论列表(0条)

保存