C++中的typedef变量

typedef variable in C++

本文关键字:变量 typedef 中的 C++      更新时间:2023-10-16

我想问一下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

CloudConstPtr是一个嵌套类型,因此您还需要用作用域来限定它:

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

但它仍然是不起作用的:因为您已经在.cpp文件中定义了它。如果是模板,则定义应在.h文件本身中可用。最简单的方法是在类本身中定义每个成员函数。不要写入.cpp文件。

getLatestCloud更改为:

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

在读取CloudConstPtr时,编译器还不知道应该在哪个范围内查找,因此需要对其进行限定。