栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

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

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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 Studioc…Father@H@@QEBA?AV0@AEBV0@@Z),函数 main 中引用了该符号 Project9 E:Microsoft Visual StudiocodeProject9main.obj 1
错误
LNK2019 无法解析的外部符号 "public: void __cdecl Father::prfT(void)const " (?prfT@?$Father@H@@QEBAXXZ),函数 main 中引用了该符号 Project9 E:Microsoft Visual StudiocodeProject9main.obj 1

错误原因

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

代码: 文件结构

​

Father.h
#pragma once

template
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
Father::Father(T a) {  // 作用域需要加虚拟参数列表
	this->a = a;
}

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

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

	return temp;
}

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

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

	Father f3 = f1 + f2;

	f3.prfT();

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

template
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
Father::Father(T a) {  // 作用域需要加虚拟参数列表
	this->a = a;
}

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

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

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

	return temp;
}

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


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

	Father f3 = f1 + f2;

	f3.prfT();

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

template
class Father {
public:
	Father(T a);

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

private:
	T a;
};


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

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

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

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

	return temp;
}

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

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

	Father f3 = f1 + f2;

	f3.prfT();

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

Father.h
#pragma once


template
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
Father::Father(T a) {  // 作用域需要加虚拟参数列表
	this->a = a;
}

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

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

	return temp;
}

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

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

	Father f3 = f1 + f2;

	f3.prfT();

	return 0;
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/954922.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号