【问题标题】:C++ cannot convert from type bool(class) to bool [closed]C++ 无法从 bool(class) 类型转换为 bool [关闭]
【发布时间】:2014-01-12 04:04:17
【问题描述】:

所以我有一个抽象超类 ReadWords,以及 3 个子类,FirstFilter、SecondFilter 和 ThirdFilter。

Readwords.h:

#ifndef _READWORDS_H
#define _READWORDS_H

using namespace std;
#include <string>
#include <fstream>
#include <iostream>
#include <cstdlib>

class ReadWords
{   public:

        ReadWords(char *filename);

        void close();

        string getNextWord();

        bool isNextWord();

        virtual bool filter(string word)=0;

        string getNextFilteredWord();

    private:
        ifstream wordfile;
        bool eoffound;
        string nextword;
        string fix(string word);

 };

 #endif

FirstFilter.h:

#ifndef _FIRSTFILTER_H
#define _FIRSTFILTER_H

using namespace std;
#include <string>
#include <fstream>
#include <iostream>
#include "ReadWords.h"

class FirstFilter: public ReadWords
{   public:
       FirstFilter(char *filename);
       virtual bool filter(string word)
       {
           for(int i=0; i<word.length(); i++){
                if (word[i]>='A'&&word[i]<='Z') return true;
           }
           return false;
       }
};

#endif

FirstFilter.cpp:

using namespace std;
#include "FirstFilter.h"

FirstFilter::FirstFilter(char *filename)
    :ReadWords(filename)
{
}

在主函数中,我创建了 FirstFilter、SecondFilter 和 ThirdFilter 类型的 3 个对象,我有类似的东西:

FirstFilter f1(file);
while(f1.isNextWord){
   //etc
}

我对所有 3 个对象都收到此错误:

error: cannot convert 'ReadWords::isNextWord' from type 'bool (ReadWords::)()' 
to type 'bool'|

有什么想法吗?告诉我你是否也需要 ReadWords.cpp,我没有放它,因为它有点大。

【问题讨论】:

  • isNextWord 是一个函数。如果你想调用它,它后面需要括号......除非你试图将它用作成员函数指针(这完全是另一回事)。
  • 您在此处缺少括号 while(f1.isNextWord())。投票结束。

标签: c++ function compiler-errors boolean


【解决方案1】:

而不是

while(f1.isNextWord){

其中 isNextWord 用作函数指针

while(f1.isNextWord() ){

其中 isNextWord 用作函数调用

【讨论】:

  • 该死的你说得对,现在是早上 6 点,我已经为此苦苦挣扎了很长一段时间。我有一个 5 岁孩子的注意力,我能说什么...谢谢!
猜你喜欢
  • 2020-02-10
  • 2016-08-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-17
  • 1970-01-01
相关资源
最近更新 更多