Vector find algorithm not working

I'm trying to make a program that reads a text file line-by-line, breaks it up into words, and checks a vector to see if the word is already in the vector. If the word isn't already in the vector, I need to add it to the vector and if it's already present, nothing happens.

My program correctly splits the text files up word by word, but the find algorithm is giving me issues. I have started with a blank string vector(called first_file) for the words to be added to.

I've tried all kinds of variations of std::find(first_file.begin(), first_file.end(), processed_word); However, I keep getting errors such as: Request for member 'begin' in 'first file' which is of non-class type 'std::vector<std::basic string<char> >()'

How do I get it to read the vector and check if the string processed_word is present in the vector and if it's not, add it?

Thanks for any help/suggestions :)

1
2
3
 std::find(first_file.begin(), first_file.end(), processed_word);

ERROR: Request for member 'begin' in 'first file' which is of non-class type 'std::vector<std::basic string<char> >()'
Just guessing since you didn't show all of your code.

Try Declaring your vector like so: std::vector<std::string> first_file;


Edit: Added semicolon.
Last edited on
Tried norm's suggestion above, same result/error.
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
#include<iostream>
#include<fstream>
#include<sstream>
#include<string>
#include<cctype>
#include <cmath>
#include <vector>
#include <algorithm>
#include <iterator>
#include <algorithm>
#include <cctype>

using std::cout; using std::cin; using std::endl;
using std::ifstream; using std::ofstream;
using std::istringstream; using std::ostringstream;
using std::string; using namespace std;
using std::isalpha; using std::tolower; using std::find;

#include "functions.h"



int main(int argc, char** argv) {
string file_name_one, file_name_two, word, line;
string processed_word;
std::vector <std::string> first_file();
std::vector <std::string> second_file();
int vector1_size;
int vector2_size;

cout << "Enter the name of the first file to read: ";
cin >> file_name_one;

ifstream in_file(file_name_one);

if (!in_file.eof()) //change back to while later so it goes through the entire file.
{
getline(in_file, line);
istringstream iss(line);

while(iss>>word)
{
processed_word = process_word(word); //Function that removes punctuation

find(first_file.begin(), first_file.end(), processed_word); //Placeholder, I have no idea what to do here to get my desired result (described in original post)

}
in_file.close();
}


return 0;
}
Last edited on
std::vector <std::string> first_file(); Declares a function that returns a vector.

Notice my suggestion above is lacking the parentheses "()" on the end.
So my eyes/brain have failed me. Stuff complies now, will mess around with it a little bit and report back :)

Thanks <3
Topic archived. No new replies allowed.