重複の可能性:
配列に格納されている特定のクラス オブジェクトの削除
クラス オブジェクトの配列内の特定の要素を削除しようとしています。削除したい要素をその後の要素で上書きしています。私のアルゴリズムは機能しますが、出力が正しくありません。デバッグしてコードを段階的に実行した後、オブジェクトがコピーされないようです。クラスオブジェクトをコピーする方法はありますか。コピーコンストラクターを検索して作成しようとしましたが、出力には何の影響もないようです。助けていただければ幸いです。ありがとう
以下は私のコードです
#include <iostream>
#include <string>
#include <math.h>
using namespace std;
void storeinfo() ;
void showinfo() ;
void menu() ;
void deleteinfo() ;
void displayallinfo() ;
int linsearch(string val) ;
class user
{
string firstname, lastname, currentteam, position, status ;
int age ;
public:
user() {};
user(string fname, string lname, string cteam, string pos, string stat, int age)
{
setFirstName(fname);
setLastName(lname);
setCurrentTeam(cteam);
setPosition(pos);
setStatus(stat);
setAge(age);
} ;
void setFirstName(string fname)
{firstname = fname;}
void setLastName(string lname)
{lastname = lname;}
void setCurrentTeam(string cteam)
{currentteam = cteam;}
void setPosition(string pos)
{position = pos;}
void setStatus(string stat)
{status = stat;}
void setAge(int _age)
{age = _age;}
string getFirstName()
{return firstname ;}
string getLastName()
{return lastname ;}
string getCurrentTeam()
{return currentteam ;}
string getPosition()
{return position ;}
string getStatus()
{return status ;}
int getAge()
{return age ;}
};
user player[20] ;
int arrlength = 3 ;
int main()
{
menu() ;
cin.get() ;
return 0 ;
}
void storeinfo()
{
string firstname ;
string lastname ;
string currentteam ;
string position;
string status ;
int age ;
for (int i=0; i < 3; i++)
{
cout << "\n\n Enter First Name : " ;
cin >> firstname ;
player[i].setFirstName(firstname) ;
cout << "Enter Last Name : " ;
cin >> lastname ;
player[i].setLastName(lastname) ;
cout << "Enter Player's Age : " ;
cin >> age;
player[i].setAge(age) ;
cout << "Enter Current Team : " ;
cin >> currentteam ;
player[i].setCurrentTeam(currentteam) ;
cout << "Enter Position : " ;
cin >> position ;
player[i].setPosition(position) ;
cout << "Enter Status : " ;
cin >> status ;
player[i].setStatus(status) ;
cout << "\n\n\n" ;
}
/*cout << string(50, '\n');*/
menu() ;
}
void showinfo()
{
string search;
int found ;
cout << "Please Enter The Player's Last Name : " ;
cin >> search ;
found=linsearch(search);
if (found==-1)
{
cout << "\n There is no player called " << search ;
}
else
{
cout << "\n First Name : " << player[found].getFirstName() << "\n" << "Last Name : " << player[found].getLastName() <<
"\n" << "Age : " << player[found].getAge() << "\n" << "Current Team : " << player[found].getCurrentTeam() <<
"\n" << "Position : " << player[found].getPosition() << "\n" << "Status : " << player[found].getStatus() << "\n\n";
}
cin.get() ;
menu() ;
}
void deleteinfo()
{
int arrlength = 3 ;
string search ;
int found ;
cout << "\n Delete A Player's Information \n\n" ;
cout << "Please Enter The Player's Last Name : " ;
cin >> search ;
found=linsearch(search);
if (found==-1)
{
cout << "\n There is no player called " << search ;
}
else
{
for (int i=found + 1; i < arrlength; ++i)
{
player[i-1].setFirstName(player[i].getFirstName()) ;
player[i-1].setLastName(player[i].getLastName()) ;
player[i-1].setAge(player[i].getAge()) ;
player[i-1].setCurrentTeam(player[i].getCurrentTeam()) ;
player[i-1].setPosition(player[i].getPosition()) ;
player[i-1].setStatus(player[i].getStatus()) ;
}
--arrlength ;
cout << "\n Player has been deleted." ;
}
cin.get() ;
menu() ;
}
void displayallinfo()
{
for (int i=0; i < 3; i++)
{
cout << "\n First Name : " << player[i].getFirstName() << "\n" << "Last Name : " << player[i].getLastName() <<
"\n" << "Age : " << player[i].getAge() << "\n" << "Current Team : " << player[i].getCurrentTeam() <<
"\n" << "Position : " << player[i].getPosition() << "\n" << "Status : " << player[i].getStatus() << "\n\n";
}
cin.get() ;
menu() ;
}
void menu()
{
cout << "\n\n MENU" << "\n" ;
cout << "\n A. Store Player Information" ;
cout << "\n B. Show Player Informaton" ;
cout << "\n C. Delete Player Information" ;
cout << "\n D. Display All Players";
cout << "\n Z. Exit \n\n" ;
string x = "";
cin >> x ;
if (x=="a" | x=="A")
{
storeinfo() ;
}
else if (x=="b" | x=="B")
{
showinfo() ;
}
else if (x=="c" | x=="C")
{
deleteinfo() ;
}
else if (x=="d" | x=="D")
{
displayallinfo() ;
}
else if (x=="z" | x=="Z")
{
exit(0) ;
}
else
{
cout << "Invalid Choice" ;
menu() ;
}
}
int linsearch(string val)
{
for (int j=0; j <= 3; j++)
{
if (player[j].getLastName()==val)
return j ;
}
return -1 ;
}