-4

I am currently trying to write a loop for list . my code is :

template<typename T>
void Bubblesorting(list<T> & mylist)
{ 
    typename T::const_iterator it1;
    typename T::const_iterator it2;
    for(it1=mylist.begin();it1!=mylist.end();it1++)
        for(it2=mylist.begin();it2!=mylist.end()-(it1-begin());it2++)
            if((*(std::next(it2,1))<*it2)
                swap((*(std::next(it2,1)),*it2);
        cout << *it2 << ' ';
}

Compilation fails with:

 error C2958: the left parenthesis '(' was not matched correctly

Can you please help me detect where exactly is the problem?how can I write a for loop for elements of list?

user3140486
  • 21
  • 1
  • 5
  • 1
    List iterator is not a random access so `+` operator won't work, and you are passing a reference to the `const` container while trying to mutate it inside the method. – bobah Jan 24 '14 at 12:34

3 Answers3

6

Function bodies have {} around them. In fact, you'll also need that around the body of your outer loop.

Your iterator type is also wrong, since T is the list type, not the list's element type. Further, it will need to be a const_iterator, since you pass the list in by const reference.

typename T::const_iterator it1;

Finally, neither (it2)+1 nor mylist.end()-it1 are possible since lists do not accommodate random access. You can fake it with std::advance and friends, but since traversing a list is non-trivial (due to the design of the data structure), this is made difficult for a reason.

In general I would revisit the concept of this entire function. Why not use std::list::sort?

I recommend picking from these books.

Community
  • 1
  • 1
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
3

Apart from the missing {} you need to define your iterator as typename T::const_iterator it1;.

Eric Fortin
  • 7,533
  • 2
  • 25
  • 33
  • Of all of the problems with this function, this is the least problematic. – Puppy Jan 24 '14 at 12:42
  • The question states that compilation fails and this is why it does not compile. – Eric Fortin Jan 24 '14 at 12:44
  • 1
    This is indeed the cause of two of the four reported errors. – Mike Seymour Jan 24 '14 at 12:47
  • There are other reasons why it won't compile- much more serious ones. – Puppy Jan 24 '14 at 13:56
  • While I agree, I do not think it warranted a downvote. I also do not believe there is a level of severity in compilation error. Anyway the question has been edited and all the original errors have disappeared so it is unfortunately not relevant anymore. – Eric Fortin Jan 24 '14 at 14:00
1

Presumably, your real code contains all the missing {}, otherwise there would be far more errors.

T appears to be the container type, not the value type, so the iterator type would be typename T::iterator; except that the container is const, so you can only get a const_iterator out of it. But your code tries to modify the container, which isn't possible if it's const. Perhaps you want to copy the argument into a new container, and sort and return that; or perhaps you want to take a non-const reference and sort the container in place (not returning anything).

You're trying to perform random-access operations on an iterator type that doesn't support them. List iterators are bidirectional - you can increment them and decrement them, but you can't add arbitrary amounts to them, or subtract them to give a distance.

Perhaps it would be more appropriate to use a random-access container like vector. Or perhaps you could replace it+n with std::next(it,n) and it2-it1 with std::distance(it1,it2). (Or, if you're stuck with a pre-C++11 library, write your own versions of these functions.)

Finally, it2!=mylist.end()-it1 doesn't make sense; the left-hand side is a distance, which can't be compared to an iterator. Perhaps you want end() - (it1-begin()), referring to the position as far from the end as it1 is from the beginning. More likely, you want to iterate from it1 to end()-1.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
  • thanks for clear guidance,I've just started programming, my purpose is to implement a bubblesort function for list for i = 0, … , n − 2 for j = 0, … , n − i − 2 if aj+1 < aj swap aj and aj+1 – user3140486 Jan 24 '14 at 13:00