我是 C++ 编程的新手,在执行单独编译时遇到了一个似乎无法解决的问题。我正在尝试专门化我的课程tokenize
来为特定类型(istream
)添加一个 dtor。我有以下内容:
#ifndef __TOKENIZER_H__
#define __TOKENIZER_H__
#include <fstream>
#include <string>
template <class T>
class base {
// ... some methods/member variables.
};
template <class T>
class tokenizer : public base<T> {
public:
tokenizer(T &in);
};
template <>
class tokenizer<std::ifstream> : public base<std::ifstream> {
public:
tokenizer(std::ifstream &in);
~tokenizer();
};
#endif
... 和:
#include "tokenizer.h"
#include <fstream>
#include <iostream>
#include <locale>
using std::ifstream;
using std::istream;
using std::string;
// [BASE]
// ... code for those functions.
// [TOKENIZER]
// See header file.
template <class T>
tokenizer<T>::tokenizer(T &in) : base<T>(in) { }
// See header file.
template <>
tokenizer<ifstream>::tokenizer(ifstream &in) : base<ifstream>(in) { }
// See header file.
template <>
tokenizer<ifstream>::~tokenizer() {
delete &(base<ifstream>::in);
}
// Intantiating template classes (separate compilation).
template class base<std::ifstream>;
template class base<std::istream>;
template class tokenizer<std::ifstream>;
template class tokenizer<std::istream>;
...但是我收到以下错误:
tokenizer.cc:62: error: template-id ‘tokenizer<>’ for ‘tokenizer<std::basic_ifstream<char, std::char_traits<char> > >::tokenizer(std::ifstream&)’ does not match any template declaration
tokenizer.cc:66: error: template-id ‘tokenizer<>’ for ‘tokenizer<std::basic_ifstream<char, std::char_traits<char> > >::~tokenizer()’ does not match any template declaration
我正在用 g++ 编译。如果有人可以指出我所缺少的内容和可能的解释,那就太好了。我很困惑模板如何与单独的编译(defns/decl 分离)一起工作。