Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/159.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ c++;在类中的函数中调用类定义之外的函数_C++_Variables_Scope - Fatal编程技术网

C++ c++;在类中的函数中调用类定义之外的函数

C++ c++;在类中的函数中调用类定义之外的函数,c++,variables,scope,C++,Variables,Scope,编辑: 我更新了代码,希望现在更具体。基本上,我尝试使用dlib库提取功能。我需要在函数get_features(在类定义中)中使用hashtable vocab,但我想在获取函数get_features之前为vocab赋值,如代码中所示,这不起作用。类特征提取程序由dlib库定义。我对C++和DLIB都是新手,我实际上不知道如何更好地解决我的问题。 现在,我的代码具有以下结构: #include <iostream> #include <dlib/svm_threaded

编辑: 我更新了代码,希望现在更具体。基本上,我尝试使用dlib库提取功能。我需要在函数get_features(在类定义中)中使用hashtable vocab,但我想在获取函数get_features之前为vocab赋值,如代码中所示,这不起作用。类特征提取程序由dlib库定义。我对C++和DLIB都是新手,我实际上不知道如何更好地解决我的问题。
现在,我的代码具有以下结构:

#include <iostream>

#include <dlib/svm_threaded.h>

using namespace std;
using namespace dlib;

/*
 * Read a vocabulary file and return a map of vocab
 * ex. vocab["word-1"] = 0, vocab["word+0"] = 1, vocab["word+1"] = 2
 */
std::map<std::string,int> getVocab() {

    std::map<std::string, int> vocab;
    std::vector<string> words;

    ifstream infile("filename");
    string line;
    while (getline(infile, line)) {
        words.push_back(line);
    }
    int cnt = 0;
    for (auto w : words) {
        vocab[w] = cnt;
        cnt++;
        }
    return vocab;
    }

class feature_extractor {
public:
    typedef std::vector<std::string> sequence_type;

    std::map<std::string, int> vocab = getVocab(); // if put here, it does NOT work.

    void get_features (
        feature_setter& set_feature,
        const sequence_type& sentence,
        unsigned long position
    ) const
    {
    std::map<std::string, int> vocab = getVocab(); // if put here, it works.
    set_feature(vocab[sentence[position]]);
    }


}

int main() {
    // other stuff
    structural_sequence_segmentation_trainer<feature_extractor> trainer;
    sequence_segmenter<feature_extractor> segmenter = trainer.train(samples, segments);
    // other stuff

}
#包括
#包括
使用名称空间std;
使用名称空间dlib;
/*
*读取词汇表文件并返回词汇表映射
*例如,vocab[“word-1”]=0,vocab[“word+0”]=1,vocab[“word+1”]=2
*/
std::map getVocab(){
std::map vocab;
向量词;
ifstream infle(“文件名”);
弦线;
while(getline(infle,line)){
字。推回(线);
}
int-cnt=0;
用于(自动w:字){
vocab[w]=cnt;
cnt++;
}
返回vocab;
}
类特征提取程序{
公众:
typedef std::向量序列\ u类型;
std::map vocab=getVocab();//如果放在这里,它将不起作用。
无效获取功能(
功能设置器和设置功能,
常量序列类型和句子,
无符号多头头寸
)常数
{
std::map vocab=getVocab();//如果放在这里,它会工作。
设置功能(vocab[句子[位置]);
}
}
int main(){
//其他东西
结构(顺序)(分割)(训练);;
sequence_segmenter segmenter=培训师培训(样本、片段);
//其他东西
}
有没有一种方法可以在函数get\u features中使用哈希表,而不用在get\u features中调用getVocab?也就是说,在函数获取功能并在函数内部使用之前,让变量
vocab
赋值


我尝试在f2之前调用类定义中的f1,并将哈希表分配给变量,但它似乎不起作用。任何帮助都将不胜感激。

我不知道您倾向于做什么,但您可以在函数外部将哈希表声明为全局变量,这是不推荐的。

我知道您希望使用函数getVocab初始化类功能提取程序的成员。您可以在构造函数中实现这一点(正如WhozCraig所建议的那样)。如果函数getVocab在逻辑上是相关的,并且您只需要它,这样它就不会进入全局名称空间,那么您也可以使它们成为同一个类的一部分。 例如:

class feature_extractor {

private:

    std::map<std::string, int> getVocab() {
        // your code
    }

public:
    typedef std::vector<std::string> sequence_type;

    std::map<std::string, int> vocab ; 

    feature_extractor() 
    {
        vocab = getVocab();
    }

};
类特征提取程序{
私人:
std::map getVocab(){
//你的代码
}
公众:
typedef std::向量序列\ u类型;
std::map vocab;
特征提取程序()
{
vocab=getVocab();
}
};

对冲赌注我理解你的问题,有很多方法可以做到这一点。其中之一如下:

  • 定义备用默认构造函数
  • 在所述的成员初始化列表中,根据
    getVocab()
    的结果初始化
    vocab
  • 就这样。它看起来像这样:

    class feature_extractor {
    public:
        typedef std::vector<std::string> sequence_type;
    
        std::map<std::string, int> vocab;
    
        // constructor added here.
        feature_extractor() : vocab(getVocab()) // <=== member initialized
        {
        }
    
        // ... rest of your code ...
    };
    
    这样,所有
    getVocab()
    的用户都将获得相同的对象引用。线程安全可能会成为一个问题,因此如果是这样的话,还需要额外的工作,但希望您能理解。您还可以将其与前面的方法结合起来,使
    vocab
    成员变量成为常量引用,而不是具体对象。在这种情况下,所有提取器将共享对
    getVocab()
    主体中相同静态
    vocab
    的相同引用:

    类特征提取程序{
    公众:
    typedef std::向量序列\ u类型;
    std::map const&vocab;//注意:参考
    //这里添加了构造函数。
    
    功能提取程序():vocab(getVocab())//这为什么会被否决?@Rodger你为什么投票支持它?有什么好的理由吗?是的,这是一个好问题。我也不知道怎么做。代码已经呈现,它将有一个明确的答案,等等@WhozCraig我添加了代码行,如评论所示,并将尝试你所拥有的suggested@Hai好吧,现在说得通了。我想你应该明白了st需要修改的默认构造函数..感谢您提供了多个解决方案。我尝试了两种方法。不知何故,我必须初始化构造函数中的成员:
    feature_extractor(){vocab=getVocab();}
    …但它现在可以工作。@Hai没问题。我提供的两个示例中都显示了使用成员初始化的语法。我建议这样做,特别是在后一种情况下,它不是建议;它是强制性的(引用必须始终初始化)。祝你好运。谢谢。我最终在类定义中定义了函数,并使用了构造函数。
    std::map<std::string,int> const& getVocab()
    {
        static std::map<std::string, int> vocab;
    
        if (vocab.empty())
        {
            std::vector<std::string> words;
    
            std::ifstream infile("filename");
            std::string line;
            while (getline(infile, line)) {
                words.push_back(line);
            }
            int cnt = 0;
            for (auto w : words) {
                vocab[w] = cnt;
                cnt++;
            }
        }
        return vocab;
    }
    
    class feature_extractor {
    public:
        typedef std::vector<std::string> sequence_type;
    
        std::map<std::string, int> const& vocab; // note: reference
    
        // constructor added here.
        feature_extractor() : vocab(getVocab()) // <=== member initialized
        {
        }
    
        // ... rest of your code ...
    };