使用函数模板重载输出运算符报错

使用函数模板重载输出运算符报错

1,报错提示

1>main.obj : error LNK2019: 无法解析的外部符号 “class std::basic_ostream<char,struct std::char_traits > & __cdecl operator<<(class std::basic_ostream<char,struct std::char_traits > &,class Vector const &)” (??6@YAAAV? b a s i c o s t r e a m @ D U ? basic_ostream@DU? basicostream@DU?char_traits@D@std@@@std@@AAV01@ABV?$Vector@H@@@Z),函数 _main 中引用了该符号
fatal error LNK1120: 1 个无法解析的外部命令

2,原代码

friend ostream& operator<<(ostream& out, const Vector<T>& object);

3,改后加上就不会报错

friend ostream& operator<< <T> (ostream& out, const Vector<T>& object);

4,输出运算符重载的具体实现不用加

template<typename T>
ostream& operator<<(ostream& os, const Vector<T>& object)

5,如果以上依然不能解决

template<typename T>
class Vector;//类名
template<typename T>
ostream& operator<<(ostream& out, const Vector<T>& object);//重载输出运算符
写在.h文件中类外即可

6,具体实现代码

1,Vector.h

#pragma once
#include<iostream>
using namespace std;

//template<typename T>
//class Vector;
//template<typename T>
//ostream& operator<<(ostream& out, const Vector<T>& object);


template<typename T>
class Vector    
{
	friend ostream& operator<< <T> (ostream& out, const Vector<T>& object);
public:
	Vector(int size = 128);
	~Vector();    
	Vector(const Vector& object);
	int getLenth();
	T& operator[](int index);
	Vector& operator=(Vector& object);
	
private:
	T* m_base;
	int m_len;
};
 

2,Vector.hpp


#include<iostream>
#include "Vector.h"
using namespace std;

template<typename T>
ostream& operator<<(ostream& os, const Vector<T>& object)
{
	for (int i = 0; i < object.m_len; i++) {
		os << object.m_base[i] << " ";
	}
	os << endl;
	return os;
}

template<typename T>
 Vector<T>::Vector(int size)
{
	 if (size > 0) {
		 m_len = size;
		 m_base = new T[m_len];
	 }
}

template<typename T>
Vector<T>::~Vector()
{
	if (m_base != NULL) {
		delete m_base;
		m_base = NULL;
		m_len = 0;
	}
}

template<typename T>
Vector<T>::Vector(const Vector& object)
{
	m_len = object.m_len;
	m_base = new T[m_len];

	for (int i = 0; i < m_len; i++) {
		m_base[i] = object.m_base[i];
	}
}

template<typename T>
int Vector<T>::getLenth()
{
	return m_len;
}

template<typename T>
T& Vector<T>::operator[](int index)
{
	return m_base[index];
}

template<typename T>
Vector<T>& Vector<T>::operator=(Vector<T>& object)
{
	if (m_base != NULL) {
		delete m_base;
		m_base = NULL;
		m_len = 0;
	}

	m_len = object.m_len;
	m_base = new T[m_len];

	for (int i = 0; i < m_len; i++) {
		m_base[i] = object.m_base[i];
	}
	return *this;
}

3,main函数

#include<iostream>
#include"Vector.hpp"

using namespace std;

int main() {
	Vector<int> v1(10);
	for (int i = 0; i < v1.getLenth(); i++) {
		v1[i] = i;
	}
	
	cout << v1<<endl;

	cout << "准备输出下标" << endl;
	for (int i = 0; i < v1.getLenth(); i++) {
		cout << v1[i] <<endl;
	}

	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值