代码之家  ›  专栏  ›  技术社区  ›  Kiku-Suma

如何将许多值从void(c++)返回到main

  •  0
  • Kiku-Suma  · 技术社区  · 11 年前

    有人能告诉我为什么对以下变量所做的更改没有被引入到main中吗?

    我对这个很陌生,所以请保持简单。

    如果您需要更多我的代码,请告诉我:D

    void BannedWordsArrayCreate (string filePathInBanned, vector<string> bannedWords, vector<int> bannedWordsCount, vector<int> containsBannedWordsCount ) {
    
    cout << "Please enter the file path for the banned word list. (no extension.): " << endl; //User enters file name
    cout << "E.g. C:\\Users\\John\\banned" << endl;
    cin >> filePathInBanned;
    filePathInBanned += ".txt"; //Takes User defined file name and adds .txt
    
    ifstream inFile;
    inFile.open(filePathInBanned,ios::in); //opens file
    
    if (!inFile) //if file cannot be opened: exits function. 
    {
        cerr << "Can't open input file." << filePathInBanned << endl;
        exit(1);
    }
    
    else if (inFile.is_open()) //if file opens: puts file into vector.
    {
        string bw = "nothing"; //temporary string used to signal end of file.
        while(!inFile.eof() && bw != "")
        {
            inFile >> bw;
            if (bw != "")
            {
                bannedWords.push_back(bw);
            }
        }
    }
    inFile.close();
    cout << endl << "Done!" << endl << endl;
    
    for(int i = 0; i < bannedWords.size(); i++)
    {
        bannedWordsCount.push_back(0);
        containsBannedWordsCount.push_back(0);
    }
    }
    
    2 回复  |  直到 11 年前
        1
  •  2
  •   Tony Delroy    11 年前

    这行。。。

    void BannedWordsArrayCreate (string filePathInBanned,
        vector<string> bannedWords, vector<int> bannedWordsCount,
        vector<int> containsBannedWordsCount )
    

    …需要询问变量 通过引用 (与 & 令牌)。。。

    void BannedWordsArrayCreate (string& filePathInBanned,
        vector<string>& bannedWords, vector<int>& bannedWordsCount,
        vector<int>& containsBannedWordsCount )
    

    引用基本上是原始变量的别名或替代名称(由调用者提供),因此对“引用”所做的更改实际上是在修改原始变量。

    在原始函数中,传递函数参数 按价值计算 ,这意味着调用上下文中的变量被复制,而函数只能处理这些副本-当函数返回时,对副本的任何修改都将丢失。


    分别地 !inFile.eof() 未正确使用。关于这个问题有很多堆栈溢出问答,但概括地说 eof() 只有在流知道您要转换什么之后,才能设置标志(例如,如果您尝试读入字符串,但它只能找到大量空格,则它将失败并设置 eof ,但如果您向流请求下一个字符是什么(包括空格),那么它将成功返回该字符,而无需点击/设置eof)。您可以将输入处理简化为:

    if (!(std::cin >> filePathInBanned))
    {
        std::cerr << "you didn't provide a path, goodbye" << std::endl;
        exit(1);
    }
    
    filePathInBanned += ".txt"; //Takes User defined file name and adds .txt
    
    if (ifstream inFile(filePathInBanned))
    {
        string bw;
        while (inFile >> bw)
            bannedWords.push_back(bw);
        // ifstream automatically closed at end of {} scope
    }
    else
    {
        std::cerr << "Can't open input file." << filePathInBanned << std::endl;
        exit(1);
    }
    
        2
  •  1
  •   Ben    11 年前

    每个参数都是按值传递的。这意味着当您调用函数时,将复制传入的对象。因此,当它们在函数内部发生更改时,更改将在副本上执行,而不是在传递的原始文件上执行。要解决此问题,请通过引用传递:

    void BannedWordsArrayCreate (string& filePathInBanned, vector<string>& bannedWords, vector<int>& bannedWordsCount, vector<int>& containsBannedWordsCount )
    

    &在对象类型表示要将内存地址复制到函数而不是对象之后。因此,当我们对函数内的对象进行更改时,我们正在更改传入地址处的内存。