C++类构造函数用法
Posted
技术标签:
【中文标题】C++类构造函数用法【英文标题】:C++ Class constructor usage 【发布时间】:2019-09-19 02:10:09 【问题描述】:我正在创建一个 C++ 程序来实现 Gale-Shapely 匹配算法,但我似乎无法弄清楚我的类构造函数做错了什么。我遵循我找到的指导方针,但 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;
【问题讨论】:
什么是“一般错误信息”?这些消息是真正来自您的 IDE(通常在您键入时更新),还是来自您的编译器(在您构建/尝试运行时给出)?您的编译器可能会给出更有用的错误消息。 【参考方案1】:我看不出这是如何编译的:
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 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);
【讨论】:
第一个编译是因为'abe'
和其余的是多字符文字 - 类型为 char
但实现定义的值 - 和(C++11 及更高版本)std::string
有一个接受初始化列表的构造函数以上是关于C++类构造函数用法的主要内容,如果未能解决你的问题,请参考以下文章