所以我有一个抽象的超类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)
{
}
在main函数中,我创建了3个FirstFilter,SecondFilter和ThirdFilter类型的对象,我有类似的东西:
FirstFilter f1(file);
while(f1.isNextWord){
//etc
}
我为所有3个对象收到此错误:
error: cannot convert 'ReadWords::isNextWord' from type 'bool (ReadWords::)()'
to type 'bool'|
有什么想法吗?告诉我你是否也需要ReadWords.cpp,我没有说它因为它有点大。
答案 0 :(得分:4)
而不是
while(f1.isNextWord){
其中isNextWord用作函数指针
写
while(f1.isNextWord() ){
其中isNextWord用作函数调用