我有一个具有以下结构的模板类
//CFoo.hpp (header file)
template <typename T>
class CFoo {
struct SFoo {
T *ptr;
/* rest is irrelevant */
} *foo;
public:
/* omitting irrelevant parts */
SFoo* get();
};
现在,如果我在头文件中实现方法SFoo* get()一切正常。但是,如果我将声明和定义分开,我的代码将停止处理以下编译错误。
//CFoo.cpp (source code, example 1)
/* omitting irrelevant parts */
template <typename T>
SFoo* CFoo<T>::get() { return foo; } //ERROR HERE
错误:<where-is-the-error>: error: ‘SFoo’ does not name a type
//CFoo.cpp (source code, example 2)
/* omitting irrelevant parts */
template <typename T>
CFoo<T>::SFoo* CFoo<T>::get() { return foo; } //ERROR HERE
错误:<where-is-the-error>: error: need ‘typename’ before ‘CFoo<T>::SFoo’ because ‘CFoo<T>’ is a dependent scope
我期待有关如何解决此问题的任何提示。提前致谢。