2014-01-12 142 views
0

所以,我有一個抽象超ReadWords,和3級的子類,FirstFilter,SecondFilter和ThirdFilter。C++不能從BOOL類型(類)轉化爲bool

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的3個對象和ThirdFilter,和我有類似:

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

我得到這個錯誤對所有3個對象:

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

任何想法?告訴我你是否需要ReadWords.cpp,我沒有把它放大一點。

+0

'isNextWord'是一個函數。如果你想叫它它需要後的括號...除非你想使用它作爲一個成員函數指針(這是完全是另一回事)。 – cHao

+2

你'而(f1.isNextWord())'這裏缺少括號。投票結束。 – dasblinkenlight

回答

3

代替

while(f1.isNextWord){ 

其中isNextWord被用作一個函數指針

寫入

while(f1.isNextWord()){ 

其中isNextWord被用作一個函數調用

+0

該死的你是對的,這是早上6點在這裏,我一直是這樣掙扎了好一陣子。我有一個5歲的注意力,我能說什麼......謝謝! –

相關問題