In defining my Element class I get the following error:
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&)
I’m not calling anything, I’m merely letting Element inherit from Dict. What’s the problem here?
The other error is in my Word class constructor I get the following:
In constructor ‘Word::Word(std::string)’: word1.cpp:71:
note: synthesized method ‘Element::Element()’ first required here
I’m really frustrated at this point as everything seems okay to me.
#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;
}
You need to provide the constructor taking no arguments for your class
Dict, if you are defining any constructor for it yourself.From where does the no parameter constructor for
Dictget invoked?Creates an
Wordobject by callingWord(string s). However, SinceWordis derived fromElementwhich in turn is derived fromDict, their default constructors, which take no parameters will be called as well. And you do not have a no parameter constructor defined forDictSo the error.Solution:
Either to provide a constructor for class
Dictwhich takes no parameters yourself.Or
define a constructor in
Elementwhich calls one of the parameterized constructors ofDictin the Member Initializer List.