2012-03-24 44 views
0

變量,我想請教一下在C++中的typedef變量的typedef在C++

好了,我現在正在使用PCL,我想代碼分離到的.h和.cpp

,這裏是我的.h文件

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

    ... 
    ... 

    CloudConstPtr getLatestCloud(); 

    ... 
    ... 
}; 

然後getLatestCloud()對其他.cpp文件

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

然後我得到了C4430的定義錯誤,因爲它不承認返回類型CloudConstPtr

抱歉愚蠢的問題:d

回答

2

CloudConstPtr是嵌套類型,所以你需要使用的範圍也限定它:

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

但隨後它仍然會而不是工作:這是因爲你已經在.cpp文件中定義它。在模板的情況下,定義應該在.h文件本身中可用。最簡單的方法是在類中定義每個成員函數。不要寫.cpp文件。

+1

啊,謝謝你的回答,反正我發現模板類不應該分開來的.h和.cpp 我應該把他們都在一個單一的.h代替:d – 2012-03-24 07:50:43

+0

@RezaAdhityaSaputra:是的。把它們放在單個'.h'文件中 – Nawaz 2012-03-24 07:54:36

1

更改getLatestCloud到:

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

當讀取CloudConstPtr,編譯器還不知道它應該尋找其中的範圍,因此需要加以限定。