[英]error LNK2019: unresolved external symbol
I need your help. 我需要你的帮助。 I have no idea what is wrong with the code.
我不知道代码有什么问题。 "Feature" is a template base class with pure virtual function and "AvgSentenceLength" is a child class, but it seems that the problem appears while calling "oneValueMap" function of the base class.
“功能”是具有纯虚函数的模板基类,而“ AvgSentenceLength”是子类,但是似乎在调用基类的“ oneValueMap”函数时出现了问题。 The error is:
错误是:
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)" (?oneValueMap@?$Feature@VText@@@@IAE?AV?$map@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@NU?$less@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@2@V?$allocator@U?$pair@$$CBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@N@std@@@2@@std@@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@3@N@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)" (?calculate@AvgSentenceLength@@UAE?AV?$map@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@NU?$less@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@2@V?$allocator@U?$pair@$$CBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@N@std@@@2@@std@@VText@@@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 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 avgSentenceLength.h
#include "text.h"
#include "feature.h"
class AvgSentenceLength : public Feature<Text>
{
public:
map<string, double> calculate(Text text);
};
avgSentenceLength.cpp 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
}
The problem is that your specialization of Feature<Text>::opeValueMap()
is not defined. 问题是您的
Feature<Text>::opeValueMap()
专业化未定义。
That is because the specialization is in avgSentenceLength.cpp
but the definition is in feature.cpp
. 那是因为专门化在
avgSentenceLength.cpp
而定义在feature.cpp
。 The are separated compilation units, so the definition cannot be used for the specialization. 它们是分开的编译单元,因此该定义不能用于专业化。
Generally speaking you should put all your template code, event the function definitions, in the .h file. 一般来说,您应该将所有模板代码,事件函数定义都放在.h文件中。
An alternative, if you know all the specializations that will be needed, is to add explicit instantiations of the code to feature.cpp
and let the linker to its job: 如果您知道将需要的所有专业知识,那么另一种方法是将代码的显式实例化添加到
feature.cpp
并让链接器执行其工作:
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;
}
//explicit instantiations
template map<string, double> Feature<Text>::oneValueMap(string name, double value);
template map<string, double> Feature<Foo>::oneValueMap(string name, double value);
template map<string, double> Feature<Bar>::oneValueMap(string name, double value);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.