引用函数外的变量 [英] Reference to variables outside function

查看:93
本文介绍了引用函数外的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在用C ++编写函数方面比较新,目前,我有一个函数可以接收文件并检查它的名字。如果它在文件名中包含/ ham /,我们就会增加hamCount的字数,并开始在hamMap中输入该文件中的单词。如果文件名包含/ spam,那么我们会对垃圾邮件做同样的事情。这一切都很好,除了当我尝试调用地图时,它们超出了范围。这是我的示例功能代码:



I am relatively new to writing functions in C++, currently, I have a function that will take in a file and check for it's name. If it includes "/ham/" in the filename, we then increase a word count for the hamCount and start throwing in the words from that file in a hamMap. If the filename contains, "/spam", then we do the same thing but for spam. This is all well and good, except, when I try to make calls to the maps, they are "outside of scope". Here is my example function code:

void mapAssign(string fileName) {
    string wordRead = "";
    ifstream opFile( fileName );
    if( !opFile.is_open() )
    {
        cout << "CANNOT OPEN FILE: " << fileName << endl;
    }

    if( fileName.find("/ham/") == true ) {
        while( getline(opFile, wordRead, ' ') ) {
        ++ham[wordRead];
        ++hamCount;
    }
        ++spam[wordRead];
        ++spamCount;
    }
    }
    
    opFile.close();
    opFile.clear();
    
}







main()函数是:




And the main() function is:

int main(int argc, char* argv[] ) {

    int wordCount;
    int fileStrCount;
    set<string> seenWords;
    map<string, int> ham;
    map<string, int> spam;    
    int hamCount;
    int spamCount;


    if( argc < 2 ) {
        cout <<  "ERROR: Must have atleast one filename! \n";
    }
    //The user gives a path to a directory containing "index"
    //File is used to store filename and fList stores them
    string files = "";
    vector<string> fList;

    //Storing full path of directory to later add /index
    string pathToDir = argv[1];
    string pathToIndex = pathToDir + "index";
    ifstream dirIndex( pathToIndex );

    //Push back all filenames within index to fList
    while( getline( dirIndex, files ) ) {
        fList.push_back( files );
    }

    //Iterate through filenames and open them 
    for(unsigned x = 0; x < fList.size(); x++) {
        string goHere = pathToDir + fList[x];
        mapAssign(goHere);
}








    }
    for(auto elem : ham)
    {
        cout << elem.first << " " << elem.second << "\n";
    }

    cout << endl << endl << endl;

    for(auto elem : spam)
    {
        cout << elem.first << " " << elem.second << "\n";
    }



}





编译错误是:

betaAssassin.cpp:26:11:错误:'ham'未在此范围内声明

++ ham [wordRead];



我尝试了什么:



我不知道怎么办,我试图改变位置函数但无法帮助



The compiling bug is:
betaAssassin.cpp:26:11: error: ‘ham’ was not declared in this scope
++ham[wordRead];

What I have tried:

I have no idea how to proceed, I have attempted changing the location of the function but that won't help

推荐答案

变量基本上只限于存在于包含它们的最近的花括号中:在这些括号之外它们不存在,无法访问。



因为您声明变量 ham spam main 函数内,它们在外面是完全不可见的。更糟糕的是,一旦函数结束,它们就会被销毁(因为它们是在堆栈上创建的,当你退出它时它会被回收 - 所以如果你返回一个指向它们的指针,它就会无效但仍然可以工作。



在任何函数之外声明它们,你可以在两者内部访问它们。
Variables are basically scoped to exist only within nearest set of curly brackets that contain them: outside those brackets they do not exist, and cannot be accessed.

Since you declare the variables ham and spam inside the main function, they are completely invisible outside. Worse, once the function ends, they are destroyed (because they are created on the stack and it is recycled when you exit it - so if you return a pointer to them, it is invalid but will still work.

Declare them outside any function, and you can access them inside both without problems.


这篇关于引用函数外的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆