c++ - 错误 LNK2019 : unresolved external symbol

标签 c++ unresolved-external

我需要你的帮助。我不知道代码有什么问题。 “Feature”是一个带有纯虚函数的模板基类,“AvgSentenceLength”是一个子类,但问题似乎是在调用基类的“oneValueMap”函数时出现的。错误是:

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

#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
}

最佳答案

问题是你对 Feature<Text>::opeValueMap() 的特化未定义。

那是因为特化是在avgSentenceLength.cpp但定义在feature.cpp .它们是独立的编译单元,因此定义不能用于特化。

一般来说,您应该将所有模板代码、事件函数定义放在 .h 文件中。

如果您知道所有需要的特化,另一种方法是将代码的显式实例化添加到 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);

关于c++ - 错误 LNK2019 : unresolved external symbol,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23519522/

相关文章:

c++ - 什么是 undefined reference /未解析的外部符号错误,我该如何解决?

c++ - 关于平行网格生成的问题

c++ - 如何在g++中构建静态库

c++ - 将 QPixmaps 拖入 QGraphicsScene : how to avoid 'auto' not allowed in lambda parameter

c++ - 使用 ffmpeg 无法解析的外部符号

c++ - MongoDB C++ 驱动程序 - 未解析的外部符号

c++ - 创建临时文件的多种方法 : what is the situation behind each method?

c++ - 在 C++ 中,什么样的语句不需要分号终止?

C++ 问题: "Error external ' C::C( )' referenced from C:\C++\CRP.OBJ"

c++ - 什么是 undefined reference /未解析的外部符号错误,我该如何解决?