类模板分文件编写

问题:

类模板中成员函数创建时机是在调用阶段,导致分文件编写时链接不到。

解决:

方式1:将声明和实现写到同一个文件中,并更名后缀为.hpp,hpp是约定的名称,并不是强制的。

方式2:直接包含.cpp源文件(不推荐)

person.h文件的代码:

#pragma once
#include <iostream>

using namespace std;

template<typename T1, typename T2>
class Person
{
public:
	T1 m_Name;
	T2 m_Age;

	Person(T1 name, T2 age);

	void showPerson();
};

person.cpp文件的代码:

#include <iostream>
#include "person.h"

// 构造函数的实现
template<typename T1, typename T2>
Person<T1, T2>::Person(T1 name, T2 age)
{
	m_Name = name;
	m_Age = age;
}

// 成员函数的实现
template<typename T1, typename T2>
void Person<T1, T2>::showPerson()
{
	cout << "姓名:" << m_Name << " 年龄:" << m_Age << endl;
}

main.cpp中的代码:

#include <iostream>
#include <Windows.h>
#include <string>

#include "person.h"

using namespace std;

void test()
{
	Person<string, int> p("张三", 18); // 报错:无法解析的外部命令
	p.showPerson(); // 报错:无法解析的外部命令
}


int main(void)
{
	test();
	
	system("pause");
	return 0;
}

        执行这两行代码Person<string, int> p("张三", 18); p.showPerson(); 的时候,报错:无法解析的外部命令。

解决方式1:

person.hpp中的代码:

#pragma once
#include <iostream>

using namespace std;

template<typename T1, typename T2>
class Person
{
public:
	T1 m_Name;
	T2 m_Age;

	Person(T1 name, T2 age);

	void showPerson();
};

// 构造函数的实现
template<typename T1, typename T2>
Person<T1, T2>::Person(T1 name, T2 age)
{
	m_Name = name;
	m_Age = age;
}

// 成员函数的实现
template<typename T1, typename T2>
void Person<T1, T2>::showPerson()
{
	cout << "姓名:" << m_Name << " 年龄:" << m_Age << endl;
}

main.cpp中的代码:

#include <iostream>
#include <Windows.h>
#include <string>

#include "person.hpp" // 将声明和实现写到同一个文件中,并更名后缀为.hpp

using namespace std;

void test()
{
	Person<string, int> p("张三", 18); 
	p.showPerson();
}


int main(void)
{
	test();
	
	system("pause");
	return 0;
}

解决方式2:

#include <iostream>
#include <Windows.h>
#include <string>

#include "person.cpp" // 包含.cpp源文件

using namespace std;

void test()
{
	Person<string, int> p("张三", 18); 
	p.showPerson();
}


int main(void)
{
	test();
	
	system("pause");
	return 0;
}

相关推荐

  1. 模板文件编写

    2024-03-30 13:42:04       17 阅读
  2. 模板文件编写

    2024-03-30 13:42:04       17 阅读
  3. C++模板文件编写

    2024-03-30 13:42:04       35 阅读
  4. [C++提高编程](二):模板--模板

    2024-03-30 13:42:04       21 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-03-30 13:42:04       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-30 13:42:04       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-30 13:42:04       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-30 13:42:04       18 阅读

热门阅读

  1. [C++提高编程](三):STL-string容器

    2024-03-30 13:42:04       18 阅读
  2. 高等代数复习:矩阵秩的基本公式

    2024-03-30 13:42:04       15 阅读
  3. Manticore Search 中文分词搜索入门

    2024-03-30 13:42:04       16 阅读
  4. 在ROS2-foxy环境中配置nooploop-linktrack

    2024-03-30 13:42:04       17 阅读
  5. Android JNI---入门了解

    2024-03-30 13:42:04       13 阅读
  6. 信号量或互斥锁

    2024-03-30 13:42:04       19 阅读
  7. VSCode中6个AI顶级插件

    2024-03-30 13:42:04       29 阅读
  8. Openreview公式不能正常显示

    2024-03-30 13:42:04       12 阅读
  9. linux终端介绍

    2024-03-30 13:42:04       18 阅读