Deleting specific class object in an array.

I am trying to delete a speific element in an array of class objects. i am overwriting the element i waant to delete with the eleement after it. My algorithm works but the output is not correct, after debugging it seems my objects just dont copy, is there a way to copy a class object, i have looked up copy constructors and attempted to write one but it does not seem to have any effect on the output. I would appreciate any help. thanks

below is my code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60

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);
	} ;
	user(const user& source):
		firstname(source.firstname),
		lastname(source.lastname),
		currentteam(source.lastname),
		position(source.position),
		status(source.status),
		age(source.age){};

	user& operator = (const user& source)
	{
		firstname = source.firstname;
		lastname = source.lastname ;
		currentteam = source.currentteam ;
		position = source.position ;
		status = source.status ;
		age = source.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 ;}
};




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40

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].getFirstName() = player[i].getFirstName() ;
			player[i - 1].getLastName() = player[i].getLastName() ;
			player[i - 1].getAge() == player[i].getAge() ;
			player[i - 1].getCurrentTeam() = player[i].getCurrentTeam() ;
			player[i - 1].getPosition() = player[i].getPosition() ;
			player[i - 1].getStatus() = player[i].getStatus() ;
		}

		--arrlength ;
				

		cout << "\n Player has been deleted." ;
	}

	cin.get() ;

	menu() ;
}



1
2
3
4
5
6
7
8
9
10
11

int linsearch(string val)
{
	for (int j=0; j <= 3; j++)
	{
		if  (player[j].getLastName()==val)
		 return j ;			
	}
		return -1 ;
}
You're not actually assigning anything. Your "get" functions just return an unnamed copy of whatever was stored in the current player. You wrote an assignment operator so why not just use that?

1
2
3
4
for (int i=found + 1; i < arrlength; ++i)
		{
			player[i - 1] = player[i];
		}


If you wanted to use your version you'd need to use the "set" functions like so:
1
2
3
4
5
for (int i=found + 1; i < arrlength; ++i)
		{
			player[i - 1].setFirstName(player[i].getFirstName());
			...
		}


EDIT: Also note that if player[] is a dynamically allocated array you're not actually deleting anything, you're only replacing values. The array will be just as big.
Last edited on
@blacksheep i have tried your method but i does not seem to work, when i debug the elements don't seem to copy and i now get an error saying the assignment operator must return a value.
That's because you declared it as a function returning a reference to a user.
1
2
3
4
5
6
7
8
9
10
user& operator = (const user& source)
	{
		firstname = source.firstname;
		lastname = source.lastname ;
		currentteam = source.currentteam ;
		position = source.position ;
		status = source.status ;
		age = source.age ;
                return *this; //Add this
	}


Although I'm pretty sure your assignment operator does exactly what the implicit operator (the one generated by the compiler if you don't explicitly define one) does.
thanks got it working finally
Topic archived. No new replies allowed.