Beginner, Bool in header file?

Hey guys, absolute beginner here. I'm having trouble trying to implement a section of code in a program we are working on at school. It keeps giving me an error. Thing is, our instructor showed us the code, so I'm assuming I most likely copied it in wrong.


 
bool findWord(string word, vector<string>& lexicon) {


The above snippet where eclipse starts to give me error *function definition is not allowed here.
From what I remember, our instructor had it very similar to how I have it.

It's located in my Header file. I'm using Eclipse on OSX.
Any help is appreciated.
Thanks.

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
/*
 * findWordsMain.h
 *
 *  Created on: Feb 4, 2015
 *      Author: Cisco
 */

#ifndef FINDWORDSMAIN_H_
#define FINDWORDSMAIN_H_
#include <string>
#include <vector>
#include <fstream>
#include <iostream>
#include <cstdlib>

using namespace std;

void loadLexicon(vector<string>& lexicon, string fileName);

bool findWord(string word, vector<string>& lexicon) {
	for (unsigned int i = 0; i < lexicon.size(); ++i)
		if (lexicon[i] == word)
			return true;
	return false;
}


#endif 




Heres the main just incase. A .txt file Lexicon is included in the project folder. The whole program is supposed to search for a word, "poodle", and return wether or not it is found in the .txt file.

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
#include <iostream>
#include <string>
#include <vector>
#include "findWordsMain.h"

using namespace std;

void loadLexicon(vector<string>&, string);
bool findWord(string, vector<string>&);

int main(){
	vector<string> lexicon;
	string word = "poodle";

	loadLexicon (lexicon, "enhanced North American Benchmark Lexicon.txt");

	if (findWord(word, lexicon))
		cout << word << " is a word. \n";
	else
		cout << word << " is NOT a word. \n";

	exit(1);
}

Last edited on
Functions certainly can be defined in header files. The only thing I can think of that you'd need to change is to make 'findWord' inline (since it is in a header, you will get linker errors if you try to #include it in multiple files unless it is inline).



On a side note, you don't need to prototype those functions in your cpp file (lines 8,9) if you are including the header which defines them. That's kind of the whole point of having a header.
Topic archived. No new replies allowed.