How to access first character from string returned by a getter

Write your question here.
I have a near completed sort function that will sort a linked list of nodes. The linked list has two levels, so there is a container node that links to other container nodes and each container node links to a chain of student nodes. Each student node can access a getName() method that returns the name of the student in that node. I am trying to sort the linked list alphabetically by name, so I am trying to access just the first character from the string
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
#include <algorithms>
void Sort(Container **head)
{
	int count = 0;
	Container* tmp = list;
	while (tmp)
	{ 
		count++;				//count how many students 
		tmp = tmp->next;
	}
	tmp = list;					//reset to head of list
	head = new Container*[count];		//empty array of Containers

	for(int i = 0; i < count; i++)
	{
		head[i] = tmp;					//load linked list into empty array of containers
		tmp = tmp->next;
	}

   sort(head[0]->student->getName[0], head[count]->student->getName[0]); //error 

   list = head[0];				//after sorting set head pointer to the beginning of the  newly sorted list
   for (int i = 0; i < count - 1; i++)
	head[i]->next = head[i + 1];		//string back together the newly sorted list
}

This is single method from a much larger program. As seen I am trying to use the generic ascending sort method from <algorithms>.
Last edited on
Most of that makes no sense.

Linked lists should not use the generic std::sort because "swapping" is done differently with links.
std::list has its own sort():
http://www.cplusplus.com/reference/list/list/sort/

You have a list of Containers. One would do it:
1
2
std::list<Container> something;
something.sort();

If there is no operator< for Containers, or it does not suite the need, then you pass a different comparator to the sort().

If the Container has a list as a member
1
2
3
struct Container {
  std::list<T> students;
};

... and you want to sort students of each container:
1
2
3
for ( auto& cont : something ) {
  cont.students.sort();
}



sort(head[0]->student->getName[0], head[count]->student->getName[0]); //error

Seriously? You say "error" and expect us to somehow explain the error message that you don't show?
No, if you do get an error message, then do show it.
That is the only way we can teach you how to read it.
Topic archived. No new replies allowed.