Class template function signature syntax
I have a class defined like this:
template< class TBoundingBox >
class BoundingBoxPlaneCalculator
{
typedef PlaneSpatialObject< TBoundingBox::PointDimension > PlaneSpatialObjectType;
typedef typename PlaneSpatialObjectType::Pointer PlaneSpatialObjectPointer;
std::vector<PlaneSpatialObjectPointer> GetPlanes() const{}
}
If I call the function like:
std::vector<PlaneCalculatorType::PlaneSpatialObjectPointer> planes = planeCalculator->GetPlanes();
It works fine. However, if I change the delcaration to simply
std::vector<PlaneSpatialObjectPointer> GetPlanes() const;
and then try to define the function outside of the header:
template< class TBoundingBox >
std::vector<BoundingBoxPlaneCalculator< TBoundingBox >::PlaneSpatialObjectPointer>
BoundingBoxPlaneCalculator< TBoundingBox >::GetPlanes() const
{
}
I get
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’
I also tried adding typename, but nothing changed:
template< class TBoundingBox >
std::vector<typename BoundingBoxPlaneCalculator< TBoundingBox >::PlaneSpatialObjectPointer>
BoundingBoxPlaneCalculator< TBoundingBox >::GetPlanes() co开发者_如何学编程nst
Can anyone see what is wrong with this syntax?
Thanks,
David
I pretty much copy-pasted your code but used int to replace a type you didn't define, and this works fine for me (using 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();
}
The thing is, this doesn't look really different from what you posted, except for the fact that PlaneSpatialObjectPointer
wasn't declared in your code. So either there's a typo or missing piece somewhere in your real code, or something else is afoot.
It looks like I just had to add typename in the calling function:
std::vector<typename PlaneCalculatorType::PlaneSpatialObjectPointer> planes = planeCalculator->GetPlanes();
I'm confused why it worked without it when the function was defined inside the class??
精彩评论