C++ 删除存储在数组中的特定类对象

C++ 删除存储在数组中的特定类对象,c++,arrays,class,object,C++,Arrays,Class,Object,可能重复: 我试图删除类对象数组中的特定元素。我正在用后面的元素覆盖要删除的元素。我的算法工作,但输出是不正确的,在调试和逐步检查代码后,我的对象似乎没有复制,有没有办法复制类对象,我已经查找了复制构造函数并试图编写一个,但它似乎对输出没有任何影响。我将感谢任何帮助。谢谢 下面是我的代码 #include <iostream> #include <string> #include <math.h> using namespace std; void st

可能重复:

我试图删除类对象数组中的特定元素。我正在用后面的元素覆盖要删除的元素。我的算法工作,但输出是不正确的,在调试和逐步检查代码后,我的对象似乎没有复制,有没有办法复制类对象,我已经查找了复制构造函数并试图编写一个,但它似乎对输出没有任何影响。我将感谢任何帮助。谢谢

下面是我的代码

#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 ;
}
#包括
#包括
#包括
使用名称空间std;
void storeinfo();
void showinfo();
无效菜单();
void deleteinfo();
void displayallinfo();
int-linsearch(字符串val);
类用户
{
字符串firstname、lastname、currentteam、position、status;
智力年龄;
公众:
user(){};
用户(字符串fname、字符串lname、字符串cteam、字符串pos、字符串stat、int age)
{
setFirstName(fname);
setLastName(lname);
setCurrentTeam(cteam);
设置位置(pos);
设置状态(status);
设置(年龄);
} ;
void setFirstName(字符串fname)
{firstname=fname;}
void setLastName(字符串lname)
{lastname=lname;}
void setCurrentTeam(字符串cteam)
{currentteam=cteam;}
无效设置位置(字符串位置)
{position=pos;}
无效设置状态(字符串状态)
{status=stat;}
无效设置(内部设置)
{age=_age;}
字符串getFirstName()
{返回firstname;}
字符串getLastName()
{返回lastname;}
字符串getCurrentTeam()
{返回currentteam;}
字符串getPosition()
{返回位置;}
字符串getStatus()
{返回状态;}
int getAge()
{返回年龄;}
};
用户播放器[20];
int arrlength=3;
int main()
{
菜单();
cin.get();
返回0;
}
void storeinfo()
{
字符串名;
字符串lastname;
字符串组;
串位置;
字符串状态;
智力年龄;
对于(int i=0;i<3;i++)
{
cout>名字;
player[i].setFirstName(firstname);
cout>lastname;
player[i].setLastName(lastname);
年龄;
玩家[i]。设置年龄(年龄);
cout>currentteam;
玩家[i].setCurrentTeam(currentteam);
cout>位置;
游戏者[i]。设置位置(位置);
状态;
玩家[i].setStatus(状态);

cout
player[i-1]。getFirstName()
返回实例的firstname成员变量的副本。将另一个值指定给此函数的输出不会更改实例本身中存储的值。请使用
player[i-1]。setFirstName(player[i].getFirstName()
相反。此外,您可以通过以下方式利用分配操作,而不是复制for循环中的每个成员
<代码>播放器[I-1 ] =播放器[i];

数组对这类事情确实不好。考虑使用STD::列表。这也意味着你可以去掉所有笨拙的大小硬编码。@DrewDormann我在这篇文章中询问的是复制构造函数,它只是同一个代码1)神圣的代码墙,蝙蝠侠!2)你编写的复制构造函数和赋值运算符与编译器已经免费提供给你的完全相同。3)getter用于获取,setter用于设置。@ryanbwork不会像variabl那样工作e firstname是私有的,所以它是私有的inaccesible@tarantino请看我的答案,使用setter代替head我刚刚尝试了你的方法,但似乎没有任何变化,程序成功地运行了算法,但没有给出正确的输出。我已经包含了完整的代码供你查看。谢谢你能给出一个示例测试场景吗?是吗ou使用displayallinfo()方法或使用一些打印语句进行调试?插入了多少播放机,试图删除哪个播放机,等等?我正在使用displayallinfo()进行调试方法,插入3个播放机并删除其中任何一个。只是复制/粘贴了您的代码并编译了它。删除播放机的工作原理与预期一致,因此我不确定您是如何测试以获得上述结果的。您只需删除删除方法末尾列表中的最后一个播放机。感谢您的帮助,最终使其工作正常