我需要你的帮助。我不知道代码有什么问题。 “Feature”是具有纯虚函数的模板基类,“AvgSentenceLength”是一个子类,但似乎在调用基类的“oneValueMap”函数时出现问题。错误是:错误LNK2019:无法解析的外部符号
1>avgSentenceLength.obj : error LNK2019: unresolved external symbol "protected: class std::map<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,double,struct std::less<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >,class std::allocator<struct std::pair<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const ,double> > > __thiscall Feature<class Text>::oneValueMap(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,double)" ([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]@@[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]@[email protected]@[email protected]@Z) в функции "public: virtual class std::map<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,double,struct std::less<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >,class std::allocator<struct std::pair<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const ,double> > > __thiscall AvgSentenceLength::calculate(class Text)" ([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][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)
feature.h中
#ifndef FEATURE_H
#define FEATURE_H
#include <string>
#include <map>
using namespace std;
template <class T> class Feature
{
public:
virtual map<string, double> calculate(T input) = 0;
protected:
map<string, double> oneValueMap(string name, double value);
};
#endif FEATURE_H
feature.cpp
#include "feature.h"
template <class T> map<string, double> Feature<T>::oneValueMap(string name, double value)
{
map<string, double> oneValueMap;
oneValueMap.insert(pair<string, double>(name, value));
return oneValueMap;
}
avgSentenceLength.h
#include "text.h"
#include "feature.h"
class AvgSentenceLength : public Feature<Text>
{
public:
map<string, double> calculate(Text text);
};
avgSentenceLength.cpp
#include "avgSentenceLength.h"
map<string, double> AvgSentenceLength::calculate(Text text)
{
double sum = 0.0;
string name = "AvgSentenceLength";
for (Sentence sentence: text.getText()) {
for (Token word: sentence.getText()) {
if (word.getType() == TokenType::tokenType::WORD) {
sum = sum + 1;
}
}
}
return oneValueMap(name, sum/text.getLength()); //getLength() returns int
}
变量和函数名称? – Brian
[为什么模板只能在头文件中实现?](http://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file) –