0

假设我有一个模板类IO<T>和另一个模板类MembershipFunction<T>。我需要MembershipFunction<T>*在我的东西中有一个 s的向量IO,还有一个std::map from std::stringto MembershipFunction<T>*。对我来说,用如此复杂的代码记住东西是不可能的。特别是在使用迭代器时。所以我尝试添加一些typedefs 到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);
};
4

2 回答 2

2

我复制/粘贴了你的代码,它用 gcc 编译得很好。您的模板使用没有任何问题。编译器错误说它以前没有见过该类型。我不在乎您是否包含该文件,编译器由于某种原因看不到完整的定义。前向声明可能还不够。还不清楚 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)
};
于 2012-07-13T20:12:51.740 回答
-1

您必须先定义MembershipFunction它才能使用它IO,只需确保它位于第一位。如果它们在单独的文件中,则在另一个文件#include中。

于 2012-07-13T19:56:00.767 回答