簡體   English   中英

C ++模板,在編譯時解析未定義的類型

[英]C++ templates, resolve undefined type in compile-time

假設我有一個模板類IO<T>和另一個模板類MembershipFunction<T> 我的IO事物中需要有一個MembershipFunction<T>*的向量,以及從std::stringMembershipFunction<T>*std::map 對我來說,用如此復雜的代碼記住事情是不可能的。 特別是在使用迭代器時。 因此,我嘗試向IO添加一些typedef 但是聽起來編譯器看不到嵌套模板。 錯誤在下面列出。

我該如何克服?

#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可以正常編譯。 您的模板使用沒有任何問題。 編譯器錯誤說它以前沒有看到過該類型。 我不在乎是否包含文件,由於某種原因,編譯器看不到完整的定義。 向前聲明可能還不夠。 還不清楚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)
};

您必須先定義MembershipFunction才能在IO使用它,只需確保它先出現即可。 如果它們在單獨的文件中,則#include一個在另一個文件中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM