在定义我的元素类,我得到以下错误:问题与继承和构造
no matching function for call to ‘Dict::Dict()’ word1.cpp:23:
note: candidates are: Dict::Dict(std::string) word1.cpp:10:
note: Dict::Dict(const Dict&)
我没有要求任何东西,我只是让元素从字典继承。这里有什么问题?
其他错误是我的Word类的构造函数,我得到如下:
In constructor ‘Word::Word(std::string)’: word1.cpp:71:
note: synthesized method ‘Element::Element()’ first required here
我真的很沮丧,在这一点上是一切似乎还好我。
#include <iostream>
#include <vector>
#include <sstream>
#include <string.h>
#include <fstream>
using namespace std;
class Dict {
public:
string line;
int wordcount;
string word;
vector <string> words;
Dict(string f) {
ifstream in(f.c_str());
if (in) {
while (in >> word) {
words.push_back(word);
}
}
else cout << "ERROR couldn't open file" << endl;
in.close();
}
};
class Element: public Dict { //Error #1 here
//Don't need a constructor
public:
virtual void complete(const Dict &d) = 0;
//virtual void check(const Dict &d) = 0;
//virtual void show() const = 0;
};
class Word: public Element{
public:
string temp;
Word(string s){ // Error #2 here
temp = s;
}
void complete(const Dict &d){cout << d.words[0];}
};
int main()
{
Dict mark("test.txt"); //Store words of test.txt into vector
string str = "blah"; //Going to search str against test.txt
Word w("blah"); //Default constructor of Word would set temp = "blah"
return 0;
}
这不仅仅是一个复制构造函数(他没有提供)。它提供了*任何*构造函数,用于防止创建默认构造函数。 –