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

错误内容

开发平台: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 <iostream>
#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 <iostream>
#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 <iostream>

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 <iostream>
#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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

划水猫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值