problem iterating and extcracting instance from a vector

I have just been learning about vector following prior advice on this forum in regards to this code.
In essence, I am confused as to how to extract the inputted name / idtag and
match it with the name of an instance previously named and created by the user.
And then once that id is matched, to then pass that instance to another function.

I followed various tutorials regarding Vectors but am getting an ugly binary
error 2679 ‘no operator found which takes a right-hand operand of type std
vector iterator.. ‘

After googling, it appeared similar 2679’s were related to headers but that
is not the case here. Its definitely in datafunc() line 136 and doesn’t like
the [iter]. I have endeavoured to work around this or use other methods
(subscript) but a similar binary error appears no matter what alternative I
use. I have only been learning C++ for 6 weeks and the more I want to pack this
particular recent project up, the more determined I become to resolve it.


If anyone could assist me in regards to the problem area in datafunc() (and
or any other of my 9999 faults) it would be greatly appreciated. Thank you.


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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include <iostream>
#include "cstdio"
#include <cctype>
#include <ctime>
#include <string>
#include <vector>
#include <algorithm>
#include <iterator>

class People {
  public:	  
    People(): 
		 attribute (0),
		 attributeB (0),
		 attributeC (0)
	 {}
	 int attribute;
	 int attributeB;
	 int attributeC;

	  void setId(std::string tag) { id = tag;  }                   
	  std::string getid() {return  id;}                                                       
	protected:
    std::string id; 
};

 
class Shopowners : public People {
	
  public:
	  
    Shopowners()
    {
	 attribute = 25;
	 attributeB = 44;
     id;
	}	
};

class Policemen : public People {
	
  public:
	  
    Policemen()
    {
	 attribute = 13;
	 attributeC = 55;
     id;
	}	
};

Policemen copperOne;
Shopowners ownerOne;

std::vector<People*> people;
//std::vector<People*>::const_iterator iter;
std::vector<People*>::iterator iter;  

void selection();
void datafunc();
void shops();
void cops();
int randomEvents();

void move()   
{   
	char yesno = 'y';
	std::cout << "If you want to move a car or person does it have a name-sticker on it? y/n \n";
	std::cin>>yesno;
	if (yesno=='y') {datafunc();}     
	else  selection();
}

void selection()
{
	int choice;
	std::cout<<"   Which toy do you want to move?\n";
	std::cout<< "            1. A shopowner.\n";
	std::cout<< "            2. A policeman.\n";
	std::cout<< "            3. A car.\n";
	std::cin>> choice;
  
   switch (choice){

   case 1: shops();
	   break;
   case 2: cops();
	   break;
   //case 3 / 4 ,5 ...2043 etc. split up into sub menus
	   }  
}

void shops()
{
	std::string name; 
	std::cout << "Enter the name you want to give him. \n";   
	std::cin.ignore(256, '\n');
	std::getline (std::cin, name);    
	Shopowners* ownerOne = new Shopowners; 
	ownerOne->setId (name);  
	people.push_back (ownerOne);
	datafunc();
}

void cops()
{
	std::string name; 
	std::cout << "Enter the name you want to give him. \n";   
	std::cin.ignore(256, '\n');
	std::getline (std::cin, name);    
	Policemen* copperOne = new Policemen; 
	copperOne->setId (name);  
	people.push_back (copperOne);
	datafunc();
	
}

void randomEvents (People &personA)      
{	  
    std::cout << personA.attribute<<"\n\n";  //example ends by printing attribute
}

// In datafunc, Im trying to match inputted 'name' with the id of the derived // class instance, and then once a match is found; pass the inherited instance
// into randomEvents

void datafunc ()   
{             
	   std::string idtag;
	   std::cout<< "Enter name on sticker. \n";
	   std::getline (std::cin, idtag); 
	      
        People* person = NULL;
		
		iter = find (people.begin(), people.end(), idtag);
		{
			idtag = people[*iter];   //no clue what Im doing here
	     
		if(idtag == ownerOne.getid()) { person = &Shopowners();} 
		if(idtag == copperOne.getid()) { person =&Policemen();}	 
		
		}
        randomEvents(*person); 
}   
 
int main()   
{	
	move();
	// other functions in gameloop
	system ("pause");
return 0;
}

Last edited on
iter = find (people.begin(), people.end(), idtag);
people keeps pointers and you try to find string. It's incorrect.
people is the vector of *People. Waht you expect "find" to return when you pass a string as input. Atleast overload "==" operator if u want waht u want.

bool operator ==(People *f,string s)
{
return f->id==s;
}

I think this is the limitation of algorithms...we cannot make find to search in pointer of pointer

say vector<string*> people.... Can we ?
Topic archived. No new replies allowed.