我有一个folllowing问题:如何在C++中用模板化函数实现父类?
我想要实现的类结构如下:在HTMLParser.h
#ifndef HTMLPARSER_H
#define HTMLPARSER_H
#include <iostream>
#include "IParser.h"
class HTMLParser : public IParser
{
public:
HTMLParser();
~HTMLParser();
json::Object Parse(std::string const&, json::Object&);
};
#endif
家长在
IParser.h
#ifndef IPARSER_H #define IPARSER_H #include "json.h" class IParser { public: template <typename T> json::Object Parse(const T&, json::Object); }; #endif // IPARSER_H
儿童孩子
HTMLParser.cpp
#include "HTMLParser.h" HTMLParser::HTMLParser() { std::cout << "constructed" << std::endl; } HTMLParser::~HTMLParser() { std::cout << "destructed" << std::endl; } json::Object HTMLParser::Parse(std::string const& data, json::Object& object) { // do something return json::Object(); }
但是,当我想建立它,它抛出我这个错误:
error LNK2019: unresolved external symbol "public: class json::Object __thiscall
IParser::Parse<class std::basic_string<char,struct std::char_traits<char>,class
std::allocator<char> > >(class std::basic_string<char,struct std::char_traits<char>,class
std::allocator<char> > const &,class json::Object)" ([email protected][email protected]?
[email protected]@[email protected]@[email protected]@[email protected]@[email protected]@@[email protected]@[email protected]@@[email protected]?
[email protected]@[email protected]@[email protected]@[email protected]@[email protected]@[email protected]@Z) referenced in function _main
任何想法可能是错误的? 基本上我想创建接口类与模板函数哪些子类将指定和实现。
任何帮助,将不胜感激。谢谢。
模板方法不能是虚拟的。 – Jarod42 2014-11-06 16:24:06
所以我不能定义一个具有不同的第一个参数的方法,孩子们将使用特定类型来实现吗? – 2014-11-06 16:28:37