“TextDocsCompr :: LETTERS_SET”,引自:_ZN12TextDocsCmprLETTERS_SET $ non_lazy_ptr

时间:2013-11-29 16:20:26

标签: c++

这是我的头文件:

#include <vector>  // std::vector
#include <string> // std::string
#include <fstream> // std::ifstream
#include <set> // std::set

class TextDocsCmpr { 

public: 

    TextDocsCmpr(); 
    ~TextDocsCmpr(); 
    void addFile(std::string); 
    void setThreshold(double); 

private:

    std::vector<std::string> files_vec; 
    std::vector<std::string> get_file_sntncs(std::fstream&);
    std::vector<std::string> get_sntnc_wrds(const std::string&);
    double sntnc_smlrty_qtnt(std::vector<std::string>, std::vector<std::string>);
    static std::set<char> LETTERS_SET;
    double sntnc_smlrty_thrshld; 


};

我在cpp文件中的构造函数下初始化LETTERS_SET:

TextDocsCmpr::TextDocsCmpr() { 
    // Set the sentence similarity threshold to a default of 0.7
    sntnc_smlrty_thrshld = 0.7;
    // Add all the characters of LETTERS_ARR to LETTERS_SET
    const char LETTERS_ARR[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
        'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D',
        'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 
    'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '\'', '.'}; 
    for (int i = 0; i < sizeof(LETTERS_ARR)/sizeof(char); ++i)
        LETTERS_SET.insert(LETTERS_ARR[i]);
}

但由于某种原因,我收到以下错误:

“TextDocsCmpr :: LETTERS_SET”,引自: __ZN12TextDocsCmpr11LETTERS_SETE PlagiarismDetector.o中的$ non_lazy_ptr 符号未找到 collect2:ld返回1退出状态

我很抱歉问了这么多问题。任何帮助是极大的赞赏。你们是最棒的!

1 个答案:

答案 0 :(得分:0)

LETTERS_SET具有静态存储,因此您需要在成员函数之外实例化它。

std::set<char> TextDocsCmpr::LETTERS_SET;

TextDocsCmpr::TextDocsCmpr() { 
    // Set the sentence similarity threshold to a default of 0.7
    sntnc_smlrty_thrshld = 0.7;
    // Add all the characters of LETTERS_ARR to LETTERS_SET
    const char LETTERS_ARR[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
        'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D',
        'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 
    'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '\'', '.'}; 
    for (int i = 0; i < sizeof(LETTERS_ARR)/sizeof(char); ++i)
        LETTERS_SET.insert(LETTERS_ARR[i]);
}