假设我有一个模板类,IO<T>
和另一个模板类 MembershipFunction<T>
.我需要一个 vector MembershipFunction<T>*
在我的IO
东西,还有一个std::map
来自 std::string
至 MembershipFunction<T>*
.对我来说,要记住这么复杂的代码是不可能的。特别是在使用迭代器时。所以我尝试添加一些 typedef
s 至 IO
.但听起来编译器无法看到嵌套模板。下面列出了错误。
我应该怎么做才能克服?
#include <vector>
#include <map>
#include <string>
#include <utility>
#include "membership_function.h" // for the love of god!
// MembershipFunction is defined there!
#include "FFIS_global.h"
template <typename T>
class DLL_SHARED_EXPORT IO
{
private:
typedef std::pair<std::string, MembershipFunction<T>* > MapEntry; // (1)
typedef std::map<std::string, MembershipFunction<T>* > Map; // (2)
typedef std::vector<const MembershipFunction<T>* > Vector; // (3)
// And so on...
这些是错误:
(1) error: 'MembershipFunction' was not declared in this scope
(1) error: template argument 2 is invalid
(1) error: expected unqualified-id before '>' token
(2 and 3): same errors
编辑:
这是 MembershipFunction
的代码
template <typename T>
class DLL_SHARED_EXPORT MembershipFunction
{
public:
virtual T value(const T& input) const{return T();}
virtual void setImplicationMethod(const typename MFIS<T>::Implication& method);
};
最佳答案
我复制/粘贴了你的代码,它用 gcc 编译得很好。您的模板使用没有任何问题。编译器错误说它以前没有见过该类型。我不在乎你是否包含该文件,编译器出于某种原因看不到完整的定义。前向声明(forward declaration)可能还不够。也不清楚 DLL_SHARED_EXPORT 是什么,我怀疑这可能是罪魁祸首。
在你对我投反对票之前,编译这个并亲自看看:
#include <vector>
#include <map>
#include <utility>
template <typename T>
class MembershipFunction
{
public:
virtual T value(const T& input) const{return T();}
//virtual void setImplicationMethod(const typename MFIS<T>::Implication& method);
};
template <typename T>
class IO
{
private:
typedef std::pair<std::string, MembershipFunction<T>* > MapEntry; // (1)
typedef std::map<std::string, MembershipFunction<T>* > Map; // (2)
typedef std::vector<const MembershipFunction<T>* > Vector; // (3)
};
关于C++ 模板,在编译时解析未定义的类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11477213/