When i try it goes heywire and gives me 35 errors. all for the erase command. :(

for(int i=0; i<populationsize_; i++)
	{
		//check for black
		for(int j=answer_->front(); j!=answer_->back(); j++)
		{
			if(answer_[j]==population_[i][j])
			{
				feedback_[i][j].push_back("Match");
				for(int k=tempanswer_->at(i).front(); k!=tempanswer_->at(i).back(); k++)
				{
					//find first instance
					if(tempanswer_[i][k]==answer_[j])
					{
						tempanswer_[i].erase(k);
						break;
					}
				}
			}
		}
}

Here's my code when I change it. Get 8 errors for this part alone. :(

for(int i=0; i<populationsize_; i++)
{
    //check for black
    for(individual=answer_->begin(); individual!=answer_->end(); individual++)
    {
        if(answer_->at(individual)==population_->at(i).at(individual))
        {
            feedback_[i][individual].push_back("Match");
            for(vector<int>::iterator itr=tempanswer_->at(i).begin(); itr!=tempanswer_->at(i).back(); itr++)
            {
                //find first instance
                if(tempanswer_[i][itr]==answer_[individual])
                {
                    tempanswer_[i].erase(itr);
                    break;
                }
            }
        }
    }
}

Errors are:

Error 1 error C2664: 'const int &std::vector<_Ty>::at(unsigned int) const' : cannot convert parameter 1 from 'std::_Vector_iterator<_Ty,_Alloc>' to 'unsigned int' c:\users...\documents\visual studio 2008\projects\cs 561\mastermind\mastermind\mastermind.cpp 65 Mastermind
Error 2 error C2664: 'const int &std::vector<_Ty>::at(unsigned int) const' : cannot convert parameter 1 from 'std::_Vector_iterator<_Ty,_Alloc>' to 'unsigned int' c:\users...\documents\visual studio 2008\projects\cs 561\mastermind\mastermind\mastermind.cpp 65 Mastermind
Error 3 error C2679: binary '[' : no operator found which takes a right-hand operand of type 'std::_Vector_iterator<_Ty,_Alloc>' (or there is no acceptable conversion) c:\users...\documents\visual studio 2008\projects\cs 561\mastermind\mastermind\mastermind.cpp 67 Mastermind
Error 4 error C2228: left of '.push_back' must have class/struct/union c:\users...\documents\visual studio 2008\projects\cs 561\mastermind\mastermind\mastermind.cpp 67 Mastermind
Error 5 error C2679: binary '!=' : no operator found which takes a right-hand operand of type 'int' (or there is no acceptable conversion) c:\users...\documents\visual studio 2008\projects\cs 561\mastermind\mastermind\mastermind.cpp 68 Mastermind
Error 6 error C2679: binary '[' : no operator found which takes a right-hand operand of type 'std::_Vector_iterator<_Ty,_Alloc>' (or there is no acceptable conversion) c:\users...\documents\visual studio 2008\projects\cs 561\mastermind\mastermind\mastermind.cpp 71 Mastermind
Error 7 error C2677: binary '[' : no global operator found which takes type 'std::_Vector_iterator<_Ty,_Alloc>' (or there is no acceptable conversion) c:\users...\documents\visual studio 2008\projects\cs 561\mastermind\mastermind\mastermind.cpp 71 Mastermind
Error 8 error C2664: 'std::_Vector_iterator<_Ty,_Alloc> std::vector<_Ty>::erase(std::_Vector_const_iterator<_Ty,_Alloc>)' : cannot convert parameter 1 from 'std::_Vector_iterator<_Ty,_Alloc>' to 'std::_Vector_const_iterator<_Ty,_Alloc>' c:\users...\documents\visual studio 2008\projects\cs 561\mastermind\mastermind\mastermind.cpp 73 Mastermind

Edited 3 Years Ago by pritaeas: removed name, fixed formatting

Most of this code looks just plain wrong, so it is unsurprising that you're getting errors! I think you need to read up a bit on the correct use of iterators.

For example, the statement starting:

if(answer_->at(individual) .....

is incorrect. You can't pass an iterator to at().
Besides, the iterator 'individual' already points to an instance of whatever variables are contained inside answer_. (That is, assuming you've correctly declared individual as an iterator...That portion of code hasn't been posted!)

Anyway, 'answer_->at(individual)' is completely wrong!

At the start of your for loop, the iterator points to the first item in the container (answer_->begin()) and it loops until the iterator reaches the end of the container.

There are several other fundamental problems with your code.

Initially it looks to me as if you have two containers that you are trying to traverse and you want to compare the contents of one against the other. But without knowing exactly what you're trying to do or what kind of variables are stored in your containers, it's kinda difficult to help you at this stage.

If you could post a little more code, or describe in more detail what you are storing in the std::library containers and what you are trying to achieve...That would allow us to understand what you're trying to do and will help us to help you.

From what I've seen so far, if my assumptions are correct; you need to perhaps try using two nested for loops. The outer loop should use an iterator to dereference the items in one container, the inner loop should dereference the items in the other. Code inside the nested loop can then be used to compare the two items and then perform any additional tasks should a match be found.

For example, take a look at this little snippet:

// We have two vectors of ints
std::vector<int> vec1;
std::vector<int> vec2;
...
// Some code goes here to populate the two vectors with values
// Perhaps vec1 is populated from a file and vec2 is populated
// via user input!
...

// Now we want to compare the values in the two vectors
for(std::vector<int>::iterator it1 = vec1.begin(); it1!=vec1.end(); vec1++)
{
    for(std::vector<int>::iterator it2 = vec2.begin(); it2!=vec2.end(); vec2++)
    {
        if( *it1 == *it2 )
        {
            // The two items match, so do something here.....
        }
    }
}

A bit of a stab in the dark, but something similar to that may be what you need to be doing. If not, post back with some further information and I'm sure somebody here will be able to help!

Cheers for now,
Jas.

[edit] oops...mitrmkar got in there whilst I was composing my thoughts!

Edited 6 Years Ago by JasonHippy: n/a

Here is all of my code: Now I get stack error. :(.

Main.cpp

#include <iostream>
#include <vector>
#include "Mastermind.h"

int main()
{
	vector<int>* answer=new vector<int>(4);
	cout<<"enter an code\n";
	for(int i=0; i<4; i++)
	{
		int k;
		cin>>k;
		answer->push_back(k);
	}
	//initalize population
	Mastermind a(5);
	//set answer
	a.setanswer(answer);
	a.showpopulation();
	a.evaluateblackandwhite();
	
	return 0;
}

Mastermind.h

#include <vector>
#include <string>

using namespace std;
class Mastermind
{
public:
	Mastermind(int populationsize);

	void showpopulation();

	void setanswer(vector<int>* answer);

	void evaluateblackandwhite();

	void calculatepopulationfitness();
	
	int evaluatepopulation();

	int	sum(int index);

private:
	int populationsize_;

	vector<int>* answer_;

	vector<vector<int>>* tempanswer_;

	vector<int>* fitness_;

	vector<vector<int>>* population_;

	vector<int>* totalnumberofpegs_;

	int* totalnumberofblackpegs_;

	int* totalnumberofwhitepegs_;

	//iterator through temp array


	//iterator through totalnumberofblackpegs
	vector<int>::iterator blackpegs;

	//iterator through totalnumberofwhitepegs
	vector<int>::iterator whitepegs;

	//iterate through population
	vector<vector<int>>::iterator pop;

	//iterate through individual
	vector<int>::iterator individual;

	vector<vector<string>>* feedback_;

	int randomizenumber();
};

inline bool operator ==(vector<string> a, string b)
{
	if(a==b)
	{
		return true;
	}
	else
	{
		return false;
	}
}

Mastermind.cpp

#include "Mastermind.h"
#include <iostream>

Mastermind::Mastermind(int populationsize)
{
	populationsize_=populationsize;
	population_=new vector<vector<int>>(populationsize_,NULL);
	for(int i=0; i<populationsize_; i++)
	{
		for(int j=0; j<4; j++)
		{
			population_->at(i).push_back(randomizenumber());
		}
	}
	answer_=new vector<int>(4);
	fitness_=new vector<int>(populationsize_);
	totalnumberofpegs_= new vector<int>(populationsize_);
	totalnumberofblackpegs_=new int[populationsize_];
	totalnumberofwhitepegs_=new int[populationsize_];
}

void Mastermind::showpopulation()
{
	for(pop=population_->begin(); pop!=population_->end(); ++pop)
	{
		for(individual=(*pop).begin(); individual!=(*pop).end(); ++individual)
		{
			cout<<*individual<<" ";
		}
		cout<<endl;
	}
}

void Mastermind::setanswer(vector<int>* answer)
{
	answer_=answer;
}

void Mastermind::evaluateblackandwhite()
{
	//copy answer to temp vector
	tempanswer_=new vector<vector<int>>(populationsize_,NULL);
	for(int i=0; i<populationsize_; i++)
	{
		for(int j=0; j<4; j++)
		{
			tempanswer_->at(i).push_back(answer_->at(j));
		}
	}
	//create feedback array
	feedback_=new vector<vector<string>>(populationsize_,NULL);
	for(int i=0; i<populationsize_; i++)
	{
		for(int j=0; j<4; j++)
		{
			feedback_->at(i).push_back("");
		}
	}

	for(int i=0; i<populationsize_; i++)
	{
		//check for black
		for(int j=answer_->front(); j!=answer_->back(); j++)
		{
			if(answer_[j]==population_[i][j])
			{
				feedback_[i][j].push_back("Match");
				for(int k=tempanswer_->at(i).front(); k!=tempanswer_->at(i).back(); k++)
				{
					//find first instance
					if(tempanswer_[i][k]==answer_[j])
					{
						tempanswer_[i][k].erase(tempanswer_->at(i).begin()+k);
						//erase data at that position
						break;
					}
				}
			}
		}
		//check for white(now go back and 
		//check for tempanswer but in wrong place
		for(int l=population_->at(i).front(); l<population_->at(i).back(); l++)
		{
			if(feedback_->at(i).at(l)=="")
			{
				for(int m=0; m<tempanswer_[i].size(); m++)
				{
					if(population_[i][l]==tempanswer_[i][m])
					{
						feedback_[i][l].push_back("Present");
						tempanswer_[i].erase(tempanswer_[i].begin()+m);
						break;
					}
				}
			}
		}

		//tally up feedback
		for(int a=0; a<feedback_[i].size(); a++)
		{
			if(feedback_[i][a]=="Match")
			{
				totalnumberofblackpegs_[i]++;
			}
			else if(feedback_[i][a]=="Present")
			{
				totalnumberofwhitepegs_[i]++;
			}
			else
			{
				totalnumberofwhitepegs_[i]=totalnumberofwhitepegs_[i];
				totalnumberofblackpegs_[i]=totalnumberofblackpegs_[i];
			}
		}
		std::cout<<"after\n";
		for(int p=0; p<populationsize_; p++)
		{
			cout<<totalnumberofblackpegs_[p];
		}
	}
}

/*void Mastermind::calculatepopulationfitness()
{
	//found fitness function online
	for(int k=0; k<populationsize_; k++)
	{
		fitness_[k]=((2*(totalnumberofblackpegs_[k]))+totalnumberofwhitepegs_[k])+sum(k);
	}
	for(int k=0; k<populationsize_; k++)
	{
		std::cout<<fitness_[k]<<std::endl;
	}
}

int Mastermind::evaluatepopulation()
{
	int mxm =(int)fitness_[0];
	for (int i=0; i<populationsize_; i++) 
	{
		if (fitness_[i]>mxm) 
		{
			mxm = fitness_[i];
		}
	}
	return mxm;
}

int Mastermind::sum(int index)
{
	int sum=0;
	for(int i=1; i<totalnumberofpegs_[index]-1; i++)
	{
		sum=sum+i;
	}
	return sum;
}*/

int Mastermind::randomizenumber()
{
	int d=rand()% 6;
	return d;
}

its doesn't like me doing matching for some reason when it is declared as a string at the end where I of the blackandwhite method.

problem is with evaluate black and white method.

Edited 6 Years Ago by lotrsimp12345: n/a

problem is somewhere in this part of code.

for(int i=0; i<populationsize_; i++)
	{
		//check for black
		for(int j=answer_->front(); j!=answer_->back(); j++)
		{
			if(answer_[j]==population_[i][j])
			{
				feedback_[i][j].push_back("Match");
				for(int k=tempanswer_->at(i).front(); k!=tempanswer_->at(i).back(); k++)
				{
					//find first instance
					if(tempanswer_[i][k]==answer_[j])
					{
						tempanswer_[i][k].erase(tempanswer_->at(i).begin()+k);
						//erase data at that position
						break;
					}
				}
			}
		}
		//check for white(now go back and 
		//check for tempanswer but in wrong place
		/*for(int l=population_->at(i).front(); l<population_->at(i).back(); l++)
		{
			if(feedback_->at(i).at(l)=="")
			{
				for(int m=0; m<tempanswer_[i].size(); m++)
				{
					if(population_[i][l]==tempanswer_[i][m])
					{
						feedback_[i][l].push_back("Present");
						tempanswer_[i].erase(tempanswer_[i].begin()+m);
						break;
					}
				}
			}
		}
		
		//tally up feedback
		for(int a=0; a<feedback_[i].size(); a++)
		{
			if(feedback_[i][a]=="Match")
			{
				totalnumberofblackpegs_[i]++;
			}
			else if(feedback_[i][a]=="Present")
			{
				totalnumberofwhitepegs_[i]++;
			}
			else
			{
				totalnumberofwhitepegs_[i]=totalnumberofwhitepegs_[i];
				totalnumberofblackpegs_[i]=totalnumberofblackpegs_[i];
			}
		}
		
	}
	std::cout<<"after\n";
	for(int p=0; p<populationsize_; p++)
	{
		cout<<totalnumberofblackpegs_[p];
	}*/

I'm still not 100% certain on what you're doing in this program. Evidently it's some take on the game Mastermind, but the code is poorly designed and rather haphazardly implemented, if I may be so bold!

One thing that immediately strikes me is your operator == overload in Mastermind.h.
You're attempting to compare a vector of strings with a string?...There's no way that'll work, that'll just get stuck in infinite recursion. Every time it hits 'if(a==b)' the == overload will get called again. (and again, and again, and again etc..)

If you really need to compare a string against a vector of strings, perhaps you need to do something like this:

inline bool operator ==(vector<string> a, string b)
{
	for(std::vector<string>::iterator it = a.begin(); it!=a.end(); it++)
		if( (*it)==b)
			return true;

	return false;
}

Which would iterate through the strings in a and return true if one of them matched b. Alternatively, you could modify it to return true if all of the strings match b, depending on what you want the overload to do!

But that's only one of the problems.
As you pointed out, there is also a crash at line 65, where it doesn't like the ==, but look at what you're comparing....
Try rewriting that section like this and see what errors you get:

//check for black
		for(int j=answer_->front(); j!=answer_->back(); j++)
		{
			int ans = answer_[j];
			int pop = population_[i][j];

			//if(answer_[j]==population_[i][j])
			if(ans==pop)
			{

Try that and you'll see why that section of code is crashing!

Personally I think you need to rethink and re-write this entire function.
In fact, pretty much the whole of Mastermind.cpp could do with a bit more of a refined design. Try to redo it using iterators to dereference objects in your containers instead of using .at() or c-style array indexing [] all over the place, that might clear up some of the confusion you're having.

At the moment; beyond being incorrect, your code is hard to read and understand and will also be difficult to maintain. The logic of your solution is at best convoluted and at worst inadequate!

Cheers for now,
Jas.

thanks. I think I agree I need to rewrite the function. I am trying to writing an evolutionary algorithm to solve the mastermind problem.

This question has already been answered. Start a new discussion instead.