C ++中的typedef变量

时间:2012-03-24 04:22:20

标签: c++ templates typedef typename

我想问一下C ++中的typedef变量

好的,现在我正在使用PCL,我想将代码分成.h和.cpp

这是我的.h文件

template <typename PointType>
class OpenNIViewer
{
public:
    typedef pcl::PointCloud<PointType> Cloud;
    typedef typename Cloud::ConstPtr CloudConstPtr;

    ...
    ...

    CloudConstPtr getLatestCloud ();

    ...
    ...
};

然后在其他.cpp文件上定义getLatestCloud()

template <typename PointType>
CloudConstPtr OpenNIViewer<PointType>::getLatestCloud ()
{
    ...
}

然后我收到了C4430错误,因为它没有识别返回类型CloudConstPtr

抱歉这个愚蠢的问题:D

2 个答案:

答案 0 :(得分:2)

CloudConstPtr是嵌套类型,因此您还需要使用范围限定它:

template <typename PointType>
typename OpenNIViewer<PointType>::CloudConstPtr OpenNIViewer<PointType>::getLatestCloud ()
{
    ...
}

但是它仍然会工作:这是因为你已经在.cpp文件中定义了它。在模板的情况下,该定义应该在.h文件本身中可用。最简单的方法是在类本身中定义每个成员函数。不要写.cpp个文件。

答案 1 :(得分:1)

getLatestCloud更改为:

template <typename PointType>
typename OpenNIViewer<PointType>::CloudConstPtr
OpenNIViewer<PointType>::getLatestCloud ()
{
    ...
}

当阅读CloudConstPtr时,编译器还不知道应该查看哪个范围,因此需要进行限定。

相关问题