如何处理模板类的向量
How to handle a vector of template class?
我正在研究一个快速排序函数,该函数对从模板创建的对象向量进行排序。特别是n维空间上的点向量。这是我的Point模板:
#ifndef POINT_H
#define POINT_H
template <int dimention, typename type>
class Point{
public:
Point(){mCoords = new type[dimention];}
Point(type* pCoords);
int getDimention(){return dimention;}
// Operators
//-----------
这是快速排序函数(我还没有写实际的实现,因为我想先解决这个问题):
#ifndef QUICK_S
#define QUICK_S
#include <vector>
#include "point.h"
// Generic quicksort function that works with points of any dimention
std::vector<Point<int dimention, typename type> >
quicksort(std::vector<Point<int dimention, typename type> > unsorted)
{
// implementation --------------
我得到的错误(其中一些):
In file included from convexHull.cpp:4:0:
quicksort.h:7:47: error: wrong number of template arguments (1, should be 2)
In file included from quicksort.h:4:0,
from convexHull.cpp:4:
point.h:5:7: error: provided for ‘template<int dimention, class type> class Point’
class Point{
In file included from convexHull.cpp:4:0:
quicksort.h:7:49: error: template argument 1 is invalid
std::vector<Point<int dimention, typename type> >
如果你能指出我错的地方,我会很感激,任何建议或想法都是受欢迎的,我是一个自学成才的程序员。谢谢。 因为quicksort
可以对dimention
和type
的任何值对vector<Point<int dimention, typename type> >
进行操作,所以它是一个模板函数,必须这样声明:
template<int dimention, typename type>
std::vector<Point<dimention, type> >
quicksort(std::vector<Point<dimention, type> > unsorted)
还要注意,Point<dimention, type>
中的int
和typename
在这里被删除了。
你的快速排序定义应该像这样声明它的模板:
std::vector<typename type>
quicksort(std::vector<type> unsorted)
{
// implementation --------------
无论何时调用快速排序,都要为特定的点设置添加模板:
quicksort<Point<1,float> >(pointList);
根据Mooing Duck的注释,在这种情况下,你不需要提供你的模板类型,因为它可以由编译器推断出来:
quicksort(pointList);
相关文章:
- 找不到成员对象:没有名为get_event()的成员,也处理多态性和向量
- 在for循环中使用auto vs decltype(vec.size())来处理字符串的向量
- 在遍历处理程序的向量时注册和注销处理程序
- 在线程函数中处理数据向量时进行线程竞速
- C++如何处理同名对象的向量?
- 编译器如何处理<Type> <Type> 具有多参数的向量initializer_list
- 指针向量的向量的处理元素
- 封送处理 - 转换 std::向量<char>到字符串^ 反之亦然
- 从向量中删除元素时未处理的异常
- 处理特征中两种存储布局的固定大小矩阵的连续向量
- C++联系人处理程序与自己的动态模板向量类
- C 向量异常处理:哪一种是抛出out_of_range()的更好方法以及原因
- CUDA矢量减少以处理长度小于512的向量
- 处理媒体循环循环循环通过向量中的对象的各个方面
- OPENCV图像处理,向量下标超出范围
- 将向量转换为数组.如何处理内存
- 在处理内存中的一些长向量时如何不弄乱缓存?
- 如何在 C++ 中处理任意维向量
- 是否有一个向量可以处理非标准位长度的整数
- 向量如何处理<bool>引用和迭代器?