1

Beginner here - but i was uncertain what exactly to search for this (presumably common) question. I am working on a program where I have a given class (Dictionary). I am supposed to make a concrete class (Word) which implements Dictionary. I should mention that I am not to change anything in Dictionary.

After making a header file for Word, I define everything in word.cpp. I am unsure if I am doing this correctly, but I make the constructor read from a given file, and store the information in a public member of Word. (I understand that the vectors should be private, but I made it public to get to the root of this current issue)

dictionary.h

#ifndef __DICTIONARY_H__
#define __DICTIONARY_H__

#include <iostream>
#include <string>
#include <vector>
#include <fstream>

using namespace std;

class Dictionary
{
public:

    Dictionary(istream&);   
    virtual int search(string keyword, size_t prefix_length)=0;     
};

#endif /* __DICTIONARY_H__ */

word.h

#ifndef __WORD_H__
#define __WORD_H__
#include "dictionary.h"




class Word : public Dictionary{
public:
    vector<string> dictionary_words;
    vector<string> source_file_words;
    Word(istream &file);    
    int search(string keyword, size_t prefix_length);
    void permutation_search(string keyword, string& prefix, ofstream& fout, int& prefix_length);

};
 #endif /* __WORD_H__*/

word.cpp

#include "word.h"

    Word(istream& file) : Dictionary(istream& file)
    {
        string temp;
        while (file >> temp)
        {
            getline(file,temp);
            dictionary_words.push_back(temp);
        }

    }

In word.cpp, on the line "Word::Word(istream& file)", I get this error :' [Error] no matching function for call to 'Dictionary::Dictionary()'.

I've been told this is error is due to "Word's constructor invoking Dictionary's ", but I still don't quite grasp the idea well. I am not trying to use Dictionary's constructor, but Word's. If anyone has an idea for a solution, I would also appreciate any terms related to what is causing this issue that I could look up - I wasn't even sure how to title the problem.

jjxd
  • 11
  • 1
  • Stop using `__DICTIONARY_H__` please, refer to [identifier](http://en.cppreference.com/w/cpp/language/identifiers) – Danh Oct 28 '16 at 00:59

1 Answers1

2

Your child class should invoke parent constructor, because parent object are constructed before child. So you should write something like:

Word::Word(isteam& file) : Dictionary(file) 
{ 
   ... 
}

Seems its better described here What are the rules for calling the superclass constructor?

Community
  • 1
  • 1
lamik
  • 89
  • 1
  • 7
  • Ah, thank you for the reply. But doing so leads to this error message: word.cpp:(.text+0x22): undefined reference to `Dictionary::Dictionary(std::istream&)' – jjxd Oct 27 '16 at 23:38
  • You have a `word.cpp` file that defines the **body** of the `Word(std::istream&)` constructor. Do you also have a similar `dictionary.cpp` file that defines the **body** of the `Dictionary(std::istream&)` constructor? The error message would suggest that your project DOES NOT contain a file that defines such a body. – Remy Lebeau Oct 27 '16 at 23:58
  • I've been trying to do so for the past while, but unsuccessfully - what should i put in the parent constructor definition? I feel as if the Word one does everything I need it to do, but I must still retain Dictionary for the purpose of this assignment – jjxd Oct 28 '16 at 01:08
  • You got error because you didn't implement Dictionary constructor. You must either do it, or remove constructor declaration from Dictionary. – lamik Oct 28 '16 at 07:31