Class Inheritance / Constructor

Hey friends,
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 it should be private, but I made it public to get to the root of this current issue)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  #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__ */ 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#ifndef __WORD_H__
#define __WORD_H__
#include "dictionary.h"




class Word : public Dictionary{
public:
	vector<string> dictionary_words;
	Word(istream &file);	
	int search(string keyword, size_t prefix_length);
	int permutation_search(string keyword,string prefix, ofstream& fout,size_t prefix_length);
	
};
 #endif /* __WORD_H__*/


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include "word.h"

	Word::Word(istream& file)
	{

		while (file.eof() == false)
		{
			string temp="";
			getline(file,temp);
			dictionary_words.push_back(temp);
		}

	}

//et cetera ... 


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

Im assuming it needs help to differentiate between Word's constructor and Dictionary's? If so, how do I do this?
If not, what is the reason for this error?
I am still relatively new to this, so forgive any other mistakes
word.cpp
Line 3: Word's constructor is trying to invoke Dictionary's default constructor, but Dictionary does not have a default constructor.

Line 6: Do not loop on ! stream.eof(). This does not work the way you expect. The eof bit is set true only after you make a read attempt on the file. This means after you read the last record of the file, eof is still false. Your attempt to read past the last record sets eof, but you're not checking it there. You proceed as if you had read a good record. This will result in reading an extra (bad) record. The correct way to deal with this is to put the >> or getline operation as the condition in the while statement.
1
2
3
  while (stream >> var)   // or while (getline(stream, var))
  {  //  Good operation
  }
@AbstractionAnon,
Thank you for your reply.
The line 6 error was an easy fix, but I still don't fully understand how to solve the Line 3 error. Could you please clarify? Why would it need to invoke the default one? Why not the custom one?

Last edited on
bump: I attempted a solution -

from word.cpp
1
2
3
4
5
6
7
8
9
10
11
12
	Word::Dictionary(istream& file) //Changed from Word::Word ...
	{
		string temp;
		while (file >> temp)
		{
			getline(file,temp);
			dictionary_words.push_back(temp);
		}

	}


Is this what you've had in mind?

If so , i now get a different error!
[Error] expected ')' before '&' token

Any ideas?
Last edited on
Line 3: Word inherits from Dictionary. When you instantiate a Word instance, all inherited classes are also instantiated. Line 3 doesn't specify a constructor to use for instantiating Dictionary, therefore it will attempt to use Dictionary's default constructor. Oops, there isn't one.

Line 1 of your latest post doesn't make any sense. You're trying to declare a function in class Word called Dictionary, which doesn't have a required return type.

Lines 4 and 6 of your latest post are going to do two reads.

I think what you want is this:
1
2
3
4
5
6
7
8
//  Construct a Word instance
//  using Dictionary's custom constructor.
Word::Word (istream& file) : Dictionary (file) 
{   string temp;
		
    while (getline(file,temp)) 
	    dictionary_words.push_back(temp);		
}


However, I'm questioning your class hierarchy. Every Word instance will contain the entire dictionary. Probably not what you want.
Last edited on
Topic archived. No new replies allowed.