You are not logged in.

Dear visitor, welcome to QtForum.org. If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

Andy

Beginner

  • "Andy" is male
  • "Andy" started this thread

Posts: 3

Location: New Zealand

Occupation: Student

  • Send private message

1

Sunday, September 19th 2004, 10:43am

Template Functions

I am having trouble using a template function for shuffling an array.
I have an array of nodes that I have to shuffle and am putting them into this function:

Source code

1
2
3
4
5
6
7
8
9
10
11
template <class T> void shuffle( T *arr, int len){
  int pos;
  T temp;
  srand(time(NULL)+clock_t());
  for(int i = len-1; i > 0; i--){
    temp = arr[i];
    pos = rand()%i;
    arr[i] = arr[pos];
    arr[pos] = temp;
  }
}


The function is called like this;

Source code

1
<Node>shuffle(nodes[1], 29);


So that it shuffles postion 1 to 29. However, the compiler gives me this error:
In function `int main(int, char**)':
error: syntax error before `<' token

There is no error before the '<' token, and when I compile without trying to call the function, it works fine, but when I try to use the function, it cannot compile.

Does anybody know why?


Thanks,
Andy.

  • "wysota" is male

Posts: 4,276

Location: Warsaw, POLAND

  • Send private message

2

Sunday, September 19th 2004, 11:04am

RE: Template Functions

The function is called the wrong way... you shouldn't prepend the function's name with <Node>

Andy

Beginner

  • "Andy" is male
  • "Andy" started this thread

Posts: 3

Location: New Zealand

Occupation: Student

  • Send private message

3

Sunday, September 19th 2004, 11:47am

RE: Template Functions

Thanks wysota.