【发布时间】: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