类模板函数签名语法

时间:2011-04-25 18:20:43

标签: c++

我有一个像这样定义的类:

template< class TBoundingBox >
class BoundingBoxPlaneCalculator
{
  typedef PlaneSpatialObject< TBoundingBox::PointDimension > PlaneSpatialObjectType;
  typedef typename PlaneSpatialObjectType::Pointer      PlaneSpatialObjectPointer;
  std::vector<PlaneSpatialObjectPointer> GetPlanes() const{}
}

如果我将此功能称为:

std::vector<PlaneCalculatorType::PlaneSpatialObjectPointer> planes = planeCalculator->GetPlanes();

工作正常。但是,如果我将说明改为

  std::vector<PlaneSpatialObjectPointer> GetPlanes() const;

然后尝试在标题之外定义函数:

template< class TBoundingBox >
std::vector<BoundingBoxPlaneCalculator< TBoundingBox >::PlaneSpatialObjectPointer>
BoundingBoxPlaneCalculator< TBoundingBox >::GetPlanes() const
{

}

我得到了

itkBoundingBoxPlaneCalculator.txx:61:82: error: type/value mismatch at argument 1 in template parameter list for ‘template<class _Tp, class _Alloc> class std::vector’
itkBoundingBoxPlaneCalculator.txx:61:82: error:   expected a type, got ‘itk::BoundingBoxPlaneCalculator<TBoundingBox>::PlaneSpatialObjectPointer’

我也尝试过添加typename,但没有改变:

template< class TBoundingBox >
std::vector<typename BoundingBoxPlaneCalculator< TBoundingBox >::PlaneSpatialObjectPointer>
BoundingBoxPlaneCalculator< TBoundingBox >::GetPlanes() const

有人能看出这种语法有什么问题吗?

谢谢,

大卫

2 个答案:

答案 0 :(得分:0)

我几乎复制粘贴你的代码,但用int替换你没有定义的类型,这对我来说很好用(使用g ++ 4.4):

#include <vector>

template<class TBoundingBox>
class BoundingBoxPlaneCalculator
{
public:
    typedef int* PlaneSpatialObjectPointer;
    std::vector<PlaneSpatialObjectPointer> GetPlanes() const;
};

template<class TBoundingBox>
std::vector<typename BoundingBoxPlaneCalculator<TBoundingBox>::PlaneSpatialObjectPointer>
    BoundingBoxPlaneCalculator<TBoundingBox>::GetPlanes() const {}

int main()
{
    BoundingBoxPlaneCalculator<int> planeCalculator;
    planeCalculator.GetPlanes();
}

问题是,除了您的代码中未声明PlaneSpatialObjectPointer这一事实外,这与您发布的内容并没有太大的不同。因此,无论是在您的真实代码中某处都有拼写错误或缺失的部分,或者正在进行其他操作。

答案 1 :(得分:0)

看起来我只需要在调用函数中添加typename:

std::vector<typename PlaneCalculatorType::PlaneSpatialObjectPointer> planes = planeCalculator->GetPlanes();

我很困惑,为什么在没有它的情况下,当在类中定义函数时呢?