Я создаю программу на C ++ для реализации алгоритма сопоставления Гейла-Шейпли, но я не могу понять, что я делаю неправильно с моим конструктором классов.Я следую рекомендациям, которые нашел, но Visual Code будет продолжать давать мне общие сообщения об ошибках и, похоже, не распознает меня как определяющего два массива, которые я определяю в конструкторе.
Это файл длякласс, который я создал:
#include <string>
#include <time.h>
using namespace std
class matcher {
private:
string male[10][10];
string fmale[10][10];
//to adapt this code to a random size of members, any mention of the array index size (such as [10] will have to replaced with a new variable)
public:
matcher(string x[10], string y[10]){
for( int i = 0; i < 9; i ++){
male[i][0] = x[i];
fmale[i][0] = y[i];
}
//populates the male and female 2d arrays with the given arrays of names
srand (time(NULL));
//attempting to seed the random generator with the time value
for( int i = 0; i < 10; i++){
int used[10];
//array of used indexes, so we dont have the same name twice on a preference list
for(int z = 0; z < 10; z++){
randval = rand() % 9;
//random index between 0 and 9
while(!(find(used[0], used[9], randval))){
male[i][z] = y[randval];
//gives the male a new prefernce at the random index from the list of females
}
}
}
for( int i = 0; i < 10; i++){
int used[10];
for(int z = 0; z < 10; z++){
randval = rand() % 9;
while(!(find(used[0], used[9], randval))){
fmale[i][z] = x[randval];
}
}
}
//similar operation, except adapted for the female list
}
void generateMatches(string x[10][10],string y[10][10]){
stack <string> unMat;
//stack of strings, males to be matched
string matches[10][1]
for(i = (length(x)) - 1), i <= 0, i++){
unMat.emplace(x[i]);
}
//places the list of males on the stack to be matched
while (!unMat.empty()){
string curr = unMat.top();
//Assign the top unmatched male to the current string
indCurr = find(std::begin(male[0][0]), std::end(male[9][0]), curr);
int xCurr = indCurr % 10;
int yCUrr = indCirr / 10;
//finds the index of the unmatched top male and assigns so we can traverse the array to find the males pref
while(!(find(std::begin(male[xCurr][0]), std::end(male[xCurr][9]), NULL)))
{
for(int i = 0; i < 10. i++){
if(male[xCurr][i] == NULL){
//if this current index in the male preference is NULL, it means there has already been an unsuccessful matching attempt
//So this female was removed so that he will not propose to her
break;
}
if(int indV = find(std::begin(matches[0][1]), std::end(mathces[9][1]), male[ind][i])){
//checks to see if the list of matches rreturns a valid index for the name, meaning it is a matched female.
int fxMatches = indv % 10;
int fyMatches = indv / 10;
//1d to 2d conversion, index of female on the matching list
int mxMatches = fxMatches;
int myMatches = 0;
string nameOfMatchedMale = matches[mxMatches][myMatches];
//giving us the name of the male matched to the female we are currently looking at
string nameOfFemale = male[ind][i];
//getting the name of the female we are going to have our current male propose to
int indx = find(fmale[0][0], fmale[9][0], nameOfFemale);
//finds the index of this female on the female list
int fx = indx % 10;
int fy = indx / 10;
//id to 2d conversion
int indMale = find(fmale[fx][1], fmale[fx][9], nameOfMatchedMale);
int indMale2 = find(fmale[fx][1],fmale[fx][9], curr);
if(indMale > indMale2){
//this means current male is ranked better than the matched male, so he will propose successfuly
unMat.pop();
//removing the current male from the unmatches stack
matches[mxMatches][myMatches] = curr;
//we will place him as the females new match, and add the unmatched male to the stack
unMat.emplace(nameOfMatchedMale);
//adding the newly unmatched male to the stack
cout<<curr;
cout<<" Proposes to ";
cout<<male[xCurr][i]<<endl;
cout<<"She accepts"<<endl;
male[xCurr][i] = NULL;
//removing the female from the current males preferences to ensure we cannot be matches again
//**********Still needing printing of these events happening************
}
else{
cout<<curr;
cout<<" Proposes to ";
cout<<male[xCurr][i]<<endl;
cout<<"She rejects him :("<<endl;
male[xCurr][i] = NULL;
//current male will propose unsuccesfully, we will replace this female in his preference list as NULL, and then he will try another
}
}
else{
int iz = 0;
while(!match[iz][0] == NULL){
i++;
}
//Need to find a way to see the next open index in the matches list
match[iz][0] = curr;
match[iz][1] = male[ind][i];
cout<<curr;
cout<<" Proposes to ";
cout<<male[xCurr][i]<<endl;
cout<<"She Accepts"<<endl;
//add current male and female to matches
male[xCurr][i] = NULL;
//always remove this female from the male preferances so he will not attempt proposol to her again
}
}
}
}
cout<<"These are the complete matches:"<<endl;
for(int i = 0;i < 10, i ++){
cout<<matches[i][0];
cout<<" and ";
cout<<matches[i][1]<<endl;
//prints all of the matches
}
for(int i = 0;i < 9; i ++){
matches[i][0] = NULL;
matches[i][1] = NULL;
/*resets the matching array so that a part of an earlier function, which checks
the contents of the array to find empty space can work properly*/
}
}
Это основной:
#include <iostream>
#include <string>
#include "matcher.cpp"
using namespace std;
int main(){
string xz[10] = {'abe','bbe','cbe','dbe','ebe','fbe','gbe','hbe','ibe','jbe'};
string yz[10] = {'kbe','lbe','mbe','nbe','obe','pbe','qbe','rbe','sbe','tbe'};
matcher newSort(xz,yz);
string answer = "y";
while(answer == "y" ){
newSort.generateMatches();
cout << "would you like to redo the match ( Y:yes, N:no )? " << endl;
cin>>answer;
}
}