【Bug记录】模板生成错误:不属于vector的成员

项目场景:

在调用自己写的一个简化版优先级队列时候报语法错误:模板生成错误
在这里插入图片描述


问题描述

模板生成错误,如下图
在这里插入图片描述

原因分析:

问题的分析:刚开始怀疑写模板写错了,之后看错误列表发现是top函数不属于vector。
在这里插入图片描述


解决方案:

该问题的具体解决方案:修改为属于vector的成员函数即可
在这里插入图片描述
修改为:
在这里插入图片描述

相关代码:

// periority_queue.h
#pragma once
#include<vector>
#include<iostream>
using namespace std;

template<typename T>
struct Less
{
public:
	bool operator()(const T& x, const T& y)
	{
		return x < y;
	}
};

template<typename T>
struct Greater
{
public:
	bool operator()(const T& x, const T& y)
	{
		return x > y;
	}
};

template<class T, class Container = vector<T>, class Compare = Greater<T>>
class periority_queue
{
private:
	Container _con;
public:
	void adjust_up(int child)
	{
		Compare com;
		int parent = (child - 1) / 2;

		while (child > 0)
		{
			if (com(_con[child], _con[parent]))
			{
				swap(_con[child], _con[parent]);
				child = parent;
				parent = (child - 1) / 2;
			}
			else
			{
				break;
			}
		}
	}

	void push(const T& x)
	{
		_con.push_back(x);
		adjust_up(_con.size() - 1);
	}

	void adjust_down(int parent)
	{
		Compare com;
		int child = parent * 2 + 1;
		while (child < _con.size())
		{
			if (child + 1 < _con.size() && com(_con[child + 1], _con[child]))
			{
				child = child + 1;
			}

			if (com(_con[child], _con[parent]))
			{
				swap(_con[child], _con[parent]);
				parent = child;
				child = parent * 2 + 1;
			}
			else
			{
				break;
			}
		}
	}

	void pop()
	{
		swap(_con[0], _con[_con.size() - 1]);
		_con.pop_back();
		adjust_down(0);
	}

	size_t size()
	{
		return _con.size();
	}

	bool empty()
	{
		return _con.empty();
	}

	const T& top()
	{
		return _con.top();
	}
};
// Test.cpp
#define _CRT_SECURE_NO_WARNINGS 1
#include"priority_queue.h"

//#include<iostream>
//#include <queue>
//#include<algorithm>
//using namespace std;

//仿函数类
//template<typename T>
//struct Less
//{
//public:
//	bool operator()(const T& x, const T& y)
//	{
//		return x < y;
//	}
//};
//
//void Test()
//{
//	vector<int> v = { 1,3,4,2 };
//	vector<int>::iterator it = v.begin();
//	while (it != v.end())
//	{
//		cout << *it << " ";
//		it++;
//	}
//	cout << endl;
//
//	sort(v.begin(), v.end(), Less<int>());
//	it = v.begin();
//	while (it != v.end())
//	{
//		cout << *it << " ";
//		it++;
//	}
//}

void Test_priority_queue(void)
{
	periority_queue<int> pq;
	pq.push(2);
	pq.push(3);
	pq.push(4);
	pq.push(1);

	while (!pq.empty())
	{
		cout << pq.top() << " ";
		pq.pop();
	}
	cout << endl;
}

int main()
{
	/*priority_queue<int> ps;
	ps.push(2);
	ps.push(3);
	ps.push(4);
	ps.push(1);

	while (!ps.empty())
	{
		cout << ps.top() << " ";
		ps.pop();
	}*/

	/*priority_queue<int, vector<int>, greater<int>> pq;
	pq.push(2);
	pq.push(3);
	pq.push(4);
	pq.push(1);

	while (!pq.empty())
	{
		cout << pq.top() << " ";
		pq.pop();
	}*/

	/*vector<int> v = { 1,3,4,2 };
	vector<int>::iterator it = v.begin();
	while (it != v.end())
	{
		cout << *it << " ";
		it++;
	}
	cout << endl;

	sort(v.begin(), v.end());
	it = v.begin();
	while (it != v.end())
	{
		cout << *it << " ";
		it++;
	}*/

	/*vector<int> v = { 1,3,4,2 };
	vector<int>::iterator it = v.begin();
	while (it != v.end())
	{
		cout << *it << " ";
		it++;
	}
	cout << endl;

	sort(v.begin(), v.end(), greater<int>());
	it = v.begin();
	while (it != v.end())
	{
		cout << *it << " ";
		it++;
	}*/
	//Test();
	Test_priority_queue();

	return 0;
}

EOF

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值