Error using Constructor

Why do I keep getting this error?


In function 'int main()':
[Error] invalid use of 'Dictionary::Dictionary'


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
class Dictionary
{
	private:
		list<string> dictionary;	
		
	public:
		//CONSTRUCTORS
		Dictionary();
		Dictionary(const string& fileName);

}
//Opens file & stores words in a file dictionary
//If file doesn't open then an exception is thrown
Dictionary::Dictionary(const string& filename)
{
	Dictionary x;
	
	//Opens the file
	ifstream iFile( filename.c_str() );
	string words;
	
	//Check to see if it opens
	if( !iFile.is_open() )
	{
		cerr<< "at Dictionary::Dictionary(): File couldn't oepn. \n";
		return;
	}
	
	//Reads all word from file
	while (true)
	{
		if( iFile.eof() ) break;
		x.addWord(words);
	}
	
}  


void Dictionary::addWord(const string& str)
{
	for(list<string>::iterator it = dictionary.begin(); it!=dictionary.end();it++)
	{
		if(*it==str){
			return;
		}
	}
	dictionary.push_back(str);
}
 
int main()
{
	
	string filename = "words.txt";
	
	Dictionary diction;
	
	diction.Dictionary(filename);
        //I have also tried doing this:
        //Dictionary diction = Dictionary(filename);
        //But that gives me a blinking cursor with no end

}
Last edited on
In your while loop, you should also check for other errors; that could be the cause of your endless loop.

Also, you haven't defined the default constructor.
Last edited on
I've defined my default constructor. It creates an empty dictionary list, i just didn't post it here. Defining it still gives me this error.

Mind reading costs extra. Can you please post all of your code?
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <list>
#include <string>
#include <iostream>

class Dictionary
{
private:
    std::list<std::string> dictionary;
    std::string file_name;
    
public:
    Dictionary(const std::string );
    std::string getFileName();
};

Dictionary::Dictionary(const std::string afilename)
{
    file_name =afilename;
}

std::string Dictionary::getFileName()
{
    return file_name;
}

int main()
{
    
    std::string source = "words.txt";
    
    Dictionary diction(source);
    std::cout << "File name is " << diction.getFileName() << '\n';
}


You now need to start adding code in the constructor to lad words into the list. And that could look something (rough and ready as it is) like this, or a separate addWord() method:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Dictionary::Dictionary(const std::string afilename)
{
    file_name = afilename;
    
    std::string word;
    
    std::ifstream myfile (file_name);
    if (myfile.is_open())
    {
        while ( myfile >> word )
        {
            dictionary.push_front(word);
        }
        myfile.close();
    }
    
    else std::cout << "Unable to open file";
}


BTW <maps> are better suited to dictionaries than <lists>, but up to you.
Last edited on
iFile.eof() will always be false since you don't read anything from it, so the file pointer doesn't change, causing EOF never being reached. You can use a string to store every line (using getline(iFile, line)) and split them by whitespace (I guess you want the words) and push them to the dictionary.
Last edited on
Line 11: Needs a ;

Line 13: The comment is incorrect. No exception is thrown. You exit the constructor, but your code continues.

Line 16: You're creating a local instance of Dictionary called x. This local variable goes out of scope at line 36, thereby losing everything you've added to it.

Lines 30-34: You're going to have an infinite loop here. You never read from ifile, therefore you will never reach eof.

Line 34: You should be adding to the list called dictionary (line 4). Not the best choice of a name for the list. Not x. x does not have an addword() member function declared. You should be calling your addword() function (not qualified by x).

Line 39: You have no declaration for addword() in your class declaration.

Line 55: You're instantiating Dictionary using its default constructor, but you have no implementation of Dictionary's default constructor.

Line 57: This is not the proper way to invoke your filename constructor.

Line 55 should be:
1
2
  Dictionary diction (filename);
  //  Delete line 57 
Last edited on
Topic archived. No new replies allowed.