gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 08:00:59 24 4
gpt4 key购买 nike

假设我有一个模板类,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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com