맨 아래의 Word 클래스 정의에서 Dict의 생성자 인 Dict (string f)을 상속 할 수 있기를 원했습니다. 그러나 직접적인 상속이 아니기 때문에 직접 할 수는 없습니다. 그것은 트리를 따르고 마지막 부모는 Element 클래스입니다.직접 부모가 아닌 기본 클래스에서 생성자를 상속하는 방법
Word 클래스 (Dict)에서 Word 클래스를 상속 받아 Word 테스트 ("test.txt")를 수행 할 수 있도록 컴파일러에게 알리는 방법은 무엇입니까? 메인에서의 인스턴스화?
#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) { // I want to let Word inherit this constructor
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 {
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:
Word(string f) : Dict(f) { }; // Not allowed (How to fix?)
void complete(const Dict &d) { };
};
};
int main()
{
//Word test("test.txt");
return 0;
}