Comparing elements of two different vectors

Jan 14, 2013 at 12:32pm
I'm trying to compare a specific element of one vector, with that of another. I want to compare a user input to an input from a text file, so I have put both into separate vector<string> containers and want to see if element [i] of vector 1 is equal to element [i] of vector 2. My code looks something like that below

1
2
3
4
if ( vector1[i] == vector2[i] )
{
   do something;
}


when i compile this i get the error message
no match for 'operator=='


I've found a similar problem on another site, but the fix for that relates to comparing the entirety of both vectors, not a single element
http://www.daniweb.com/software-development/cpp/threads/168611/comparing-vectors#

Any help would be appreciated. Thanks

Jan 14, 2013 at 1:20pm
I don't see anything wrong.

Did you include <string>? You haven't posted the complete error message. Make sure the error message shows the correct types (std::string).

If that doesn't help you can post a small example that when compiled gives the error message you got. That way it will be easy for us to spot the problem.
Jan 14, 2013 at 4:21pm
Sorry about not putting everything down but there is quite a lot of it. I've put the entire program below

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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <iterator>

using namespace std;


class Question		//define new class "Question"
{
    string q;		//define private parameteres for "Question" class
	
public:
    Question(istream& in)
    {
        for (int i = 0; i < 5; ++i)
        {
            string Q_Line;
            getline(in, Q_Line);
            q.append("\n");
            q.append(Q_Line);
			
        }
    }
    friend ostream& operator<<(ostream& q_out, Question& question);	  	  			
};


ostream& operator<<(ostream& q_out, Question& question)
{
    q_out << question.q;
    return q_out; 	  	  //returns questions
}


class Answer		//define new class "Answer"
{
	string a;		//define private parameters for "Answer" class
	
public:
	Answer(istream& in)
	{
		for (int i = 0; i < 2; ++i)
		{
			string Ans_Line;
			getline(in, Ans_Line);
			a.append("\n");
			a.append(Ans_Line);
		
		}
	}
	friend ostream& operator<<(ostream& a_out, Answer& answer);
	
}; 


ostream& operator<<(ostream& a_out, Answer& answer)
{
	a_out << answer.a;
	return a_out;	  //returns answers
}


int main()
{
        vector<Question> Qlist;				
        ifstream questions_in("History_M.txt");
        while(questions_in.peek() != EOF)	   			//checks lines of text file until end of file
            Qlist.push_back( Question(questions_in) );	//adds questions to vector Qlist
		
	vector<Answer> Alist;
	ifstream answers_in("History_M_ans.txt");
	while(answers_in.peek() != EOF)
		Alist.push_back( Answer(answers_in) );
		
	int currentQuestion;
	int a = 0;
	int s = 0;
	
	vector<string> userAnswers;
	string userInput = "";
    
    for ( currentQuestion = 0; currentQuestion < 5; currentQuestion++ ) 
    {
    	cout << Qlist[currentQuestion] << endl;
    	cin >> userInput;
		userAnswers.push_back( string(userInput) );		//puts userInput string into userAnswers vector<string>
    	cout << endl << endl;

        
        if ( userAnswers[currentQuestion] == Alist[currentQuestion] )
		{
            a++; // used to increment score
        }
    } 
	
	if ( currentQuestion == 5 )
    {
		s = a * 20;

        if ( s == 100 )
        cout << "Congratulations! You got 100%" << endl;

        else
        cout << "You scored " << s << "%" << endl;
        }
		
}


the error message refers to line 92 above and reads
error: no match for 'operator==' in 'userAnswers. std::vector<_Tp,_Alloc>::operator[] [with_Tp = std::basic_string<char, std::char_traits<char>, std::allocator<char>>, _Alloc = std::allocator<std::basic_string<char,std::char_traits<char>, std::allocator<char> > >] (((unsigned int)currentQuestion)) == Alist. std::vector<_tp,_Alloc>::operator[] [with_Tp = Answer,_alloc = std::allocator<Answer>](((unsigned int)currentQuestion))'


Hope this helps
Jan 14, 2013 at 4:31pm
userAnswers is a std::vector<std::string> and Alist is a std::vector<Answer> so on line 92 you are trying to compare a std::string object to a Answer object.

If you want it to be possible to compare these two types in this order using operator== you will have to overload operator== to take a std::string as first argument and a Answer as second argument, otherwise you will have to find some other way to do the comparison.
Last edited on Jan 14, 2013 at 4:32pm
Jan 14, 2013 at 4:32pm
> if ( userAnswers[currentQuestion] == Alist[currentQuestion] )

You are trying to compare an object of type std::string with an object of type Answer for equality.
Such an operator is not defined.

One way to define it:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class answer
{
     string a;    //define private parameters for "Answer" class
	
     public:

         // ...

     inline friend bool operator== ( const Answer& ans, const std::string& str )
     { return ans.a == str ; }

     inline friend bool operator== ( const std::string& str, const Answer& ans )
     { return str == ans.a ; }

     // operator != etc.  in like manner

};

Jan 14, 2013 at 4:54pm
Thanks for the replies, that code above sorted it out :)
Topic archived. No new replies allowed.