2014-05-07 47 views
0

我需要你的帮助。我不知道代码有什么问题。 “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 
} 
+0

变量和函数名称? – Brian

+0

[为什么模板只能在头文件中实现?](http://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file) –

回答

3

的问题是,你的Feature<Text>::opeValueMap()专业化没有定义。

这是因为专业化在avgSentenceLength.cpp,但定义在feature.cpp。这些是分离的编译单元,所以定义不能用于专业化。

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

的选择,如果你知道将需要的所有专业,是对代码的明确实例添加到feature.cpp,让连接到它的工作:你为什么要使用`oneValueMap`作为

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); 
+0

非常感谢!我已经把函数定义放在.h文件中,现在它可以工作。 – user3612348

相关问题