【问题标题】:C++ void value not ignored as it ought to be (Not trying to assign a void value...)C++ void 值没有被忽略,因为它应该是(不试图分配一个 void 值......)
【发布时间】:2015-12-01 01:29:46
【问题描述】:

我的问题是: 错误是否恰好出现在未忽略 void 值的位置,还是出现在内部发生此错误的函数调用中?

例如,我遇到的具体问题...

在我的main.cpp 中,我有以下声明和函数调用:

Dictionary * D = new Dictionary(&dictionaryFile, dictionaryName);
ifstream checkFile;
...
*D->spellCheck(&checkFile, fileName, &cout);

这里的函数调用给出了错误:

“错误:应忽略的无效值”

这个特定的函数调用是在尝试使用 void 值还是在 Dictionary.cpp 中定义如下的函数内:

void Dictionary::spellCheck(ifstream * checkFile, string fileName, std::ostream * out){
    _report.setFileName(fileName);
    string end = "\n";
    int words = 0;
    int wrong = 0;
    string word;
    char currChar;
    while(*checkFile >> word){
        currChar = checkFile->get();
        while(currChar != ' ' && currChar != ',' && currChar != '.'){
            word += currChar;
            currChar = checkFile->get();
        }
        *out << word << end;
        word.clear();
    }
    /*
    _report.setWordsRead(words);
    _report.setWordsWrong(wrong);
    _report.printReport();
    */
}

提前非常感谢!

【问题讨论】:

  • 尝试将*D-&gt;spellCheck 更改为D-&gt;spellCheck。
  • 仅此而已!谢谢!
  • 运算符优先级使得一元 * 运算符适用于子表达式 D-&gt;spellCheck(&amp;checkFile, fileName, &amp;cout),从而产生 void 结果。 (*D).spellCheck(&amp;checkFile, fileName, &amp;cout) 也可以,但D-&gt;spellCheck(...) 更惯用。

标签: c++ compiler-errors


【解决方案1】:

*D-&gt;spellCheck(...) 首先调用D-&gt;spellCheck,然后尝试取消引用它的返回值。因为它返回void,所以不能取消引用返回值。

删除*。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-05-15
    • 1970-01-01
    • 2015-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-14
    相关资源
    最近更新 更多