Using own header file with classes and vectors with fstream

Hi I have my first assignment with classes and creating your own header file.
We were given a file with words and a definition next to the word. That should be read into the program. Each word and definition pair should be put in one spot in the vector. So if there are 10 words then there should be 10 spots for the pair.

Part 1 of the assignment we need to get a word from the user and then output the definition.
Part 2 is we need to sort the file alphabetically

I am having trouble for the part where you read the file and place it into the vector. And for the functions get_word(), search(), and sort().

Here are my codes so far.

.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#ifndef FUNCTIONS_H
#define FUNCTIONS_H

#include <string>

using std::string;

class dictionary{
	public:
		void get_word();		
		dictionary(string wordD, string defD);		
	
	private:
		string word, def;

};


void sort(std::string word);
void search();

#endif 

.cpp
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
#include <iostream>
#include "functions.h"

dictionary::dictionary(string wordD, string defD): word(wordD), def(defD){};
//alphabeltical order
void sort(){
//code 

}

dictionary::get_word(){
	cout <<"Please enter a word: \n";
	cin >>word;
	search(word);

}


void search(string word){ //not sure if this would work
	for (int i = 0; i > wd.size(); i++){
		if (wd[i].get_word == word){
			cout <<"The definition is " <<wd[i].get_word <<endl;
		}
		else {
			cout <<"The word is not in the dictionary\n";
		}
}

This is the main where I wrote some code
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
#include <iostream>
#include <vector>
#include <fstream>
#include "functions.h"
#include <stdlib.h>
#include <string>
#include <cstdlib>
using namespace std;

int main (int argc, char* argv[]){
	dictionary temp;
	vector <dictionary> wd;
    ifstream fin;  
    fin.open("Words_list.txt");
    if(fin.fail()){
                  cout <<"error.\n";
    }
    else{ 
        string word, def; //do'nt know if I can decalare these bec my private variables in class. 
        while (!fin.eof()){ //
              fin >> word;//the word is read in here
		getline(fin, def);//def is read in here
		                      
		dictionary temp(word, def); //wanted to put the word and def in here 
		wd.push_back(temp); //then put it in the vector
        }
    	
        fin.close(); 
}

	//get_word();
	//sort();
system ("Pause");	
return 0;
}


I am getting errors saying "no matching function for call to `dictionary::dictionary()'"
"
Sorry for the long code, I hope someone can help.
Last edited on
> "no matching function for call to `dictionary::dictionary()'"
You have defined a custom constructor, so the default no longer exists. If you need one, then you need to define it.
However, ¿do you need a default constructor? In line 11 you do dictionary temp; and never use that variable.

> 'class std::vector<dictionary, std::allocator<dictionary> >' has no member named 'pushback' "
It is called push_back
Thanks for your help. Don't I need a constructor for line 24 of my main program?
I realized I accidentally declared temp twice with dictionary
Topic archived. No new replies allowed.