C++ 模板,在编译时解析未定义的类型

标签 c++ templates

假设我有一个模板类,IO<T>和另一个模板类 MembershipFunction<T> .我需要一个 vector MembershipFunction<T>*在我的IO东西,还有一个std::map来自 std::stringMembershipFunction<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/

相关文章:

C++ 在嵌套命名空间内定义外部命名空间的函数

c++ - 将仿函数传递给仿函数

C++动态向下转换为具有模板模板参数的类模板是类模板或别名模板

c++ - 模板函数无法将 'int' 转换为 nullptr_t

c++ - 对象数组的释放?

c++ - 无法在 Visual Studio 代码中启动 C++ 调试器

C++:带有空格的cin到没有getline函数的字符串

javascript - Angular.js 加载、处理并显示通过 REST $resource 获取的动态模板

c++ - 是否可以使用 C++ 模板函数来接受任何类型 T 的集合?

c++ - 从静态成员函数推断类模板参数的模式