Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/151.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++ 如何从void(c+;+;)向main返回多个值_C++_Function_Return_Void_Multiple Value - Fatal编程技术网

C++ 如何从void(c+;+;)向main返回多个值

C++ 如何从void(c+;+;)向main返回多个值,c++,function,return,void,multiple-value,C++,Function,Return,Void,Multiple Value,有谁能告诉我为什么对以下变量所做的更改没有通过main 我对这个很陌生,所以请保持简单 如果您需要更多我的代码,请告诉我:D void BannedWordsArrayCreate (string filePathInBanned, vector<string> bannedWords, vector<int> bannedWordsCount, vector<int> containsBannedWordsCount ) { cout << "

有谁能告诉我为什么对以下变量所做的更改没有通过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);
}
}
void BannedWordsArrayCreate(字符串文件路径绑定、向量bannedWords、向量bannedwordscont、向量containsbannedwordscont){
这条线

void BannedWordsArrayCreate (string filePathInBanned,
    vector<string> bannedWords, vector<int> bannedWordsCount,
    vector<int> containsBannedWordsCount )
引用基本上是原始变量的别名或替代名称(由调用方提供),因此“对引用”所做的更改实际上是在修改原始变量

在原始函数中,函数参数通过值传递,这意味着调用上下文中的变量被复制,函数只在这些副本上工作-当函数返回时,对副本的任何修改都将丢失


另外,
!infle.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);
}
if(!(std::cin>>文件路径扫描))
{
标准:cerr bw)
横幅词。推回(bw);
//ifstream在{}范围结束时自动关闭
}
其他的
{
这条线

void BannedWordsArrayCreate (string filePathInBanned,
    vector<string> bannedWords, vector<int> bannedWordsCount,
    vector<int> containsBannedWordsCount )
引用基本上是原始变量的别名或替代名称(由调用方提供),因此“对引用”所做的更改实际上是在修改原始变量

在原始函数中,函数参数通过值传递,这意味着调用上下文中的变量被复制,函数只在这些副本上工作-当函数返回时,对副本的任何修改都将丢失


另外,
!infle.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);
}
if(!(std::cin>>文件路径扫描))
{
标准:cerr bw)
横幅词。推回(bw);
//ifstream在{}范围结束时自动关闭
}
其他的
{
这条线

void BannedWordsArrayCreate (string filePathInBanned,
    vector<string> bannedWords, vector<int> bannedWordsCount,
    vector<int> containsBannedWordsCount )
引用基本上是原始变量的别名或替代名称(由调用方提供),因此“对引用”所做的更改实际上是在修改原始变量

在原始函数中,函数参数通过值传递,这意味着调用上下文中的变量被复制,函数只在这些副本上工作-当函数返回时,对副本的任何修改都将丢失


另外,
!infle.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);
}
if(!(std::cin>>文件路径扫描))
{
标准:cerr bw)
横幅词。推回(bw);
//ifstream在{}范围结束时自动关闭
}
其他的
{
这条线

void BannedWordsArrayCreate (string filePathInBanned,
    vector<string> bannedWords, vector<int> bannedWordsCount,
    vector<int> containsBannedWordsCount )
引用基本上是原始变量的别名或替代名称(由调用方提供),因此“对引用”所做的更改实际上是在修改原始变量

在原始函数中,函数参数通过值传递,这意味着调用上下文中的变量被复制,函数只在这些副本上工作-当函数返回时,对副本的任何修改都将丢失


另外,
!infle.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);
}
if(!(std::cin>>文件路径扫描))
{
标准:cerr bw)
横幅词。推回(bw);
//ifstream在{}范围结束时自动关闭
}
其他的
{

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

void BannedWordsArrayCreate (string& filePathInBanned, vector<string>& bannedWords, vector<int>& bannedWordsCount, vector<int>& containsBannedWordsCount )
void BannedWordsArrayCreate(字符串和文件路径绑定、向量和bannedWords、向量和bannedwordscont、向量和容器绑定)
&在对象类型表示要将内存地址复制到函数而不是对象之后