2

Possible Duplicate:
Where and why do I have to put the “template” and “typename” keywords?

I'm learning about template functions. I'm try to implement a static template function that clear a list of pointers. To do it I want to use templates. Its my code:

#include <cstdio>
#include <list>
using namespace std;

class util
{
public:
    template <class ARG>
    static void CleanPointers(list<ARG> &mylist) {
        list<ARG>::iterator it;
        for (it = mylist.begin(); it != mylist.end(); it++)
        {
            ARG obj = (ARG) *it;
            delete obj;
        }
        mylist.clear();
    };

    util();
    ~util();
};

int main()
{
    list<int*> mylist;
    mylist.push_back(new int(1));
    mylist.push_back(new int(2));
    util::CleanPointers<int*>(mylist);
    return 0;
}

I recived follow compile error message and I dont understand what is the point here. :) Why need I put ; before it?

prog.cpp: In static member function ‘static void util::CleanPointers(std::list<ARG,         std::allocator<_Tp1> >&)’:
prog.cpp:10: error: expected `;' before ‘it’
prog.cpp:11: error: ‘it’ was not declared in this scope
prog.cpp: In static member function ‘static void util::CleanPointers(std::list<ARG,     std::allocator<_Tp1> >&) [with ARG = int*]’:
prog.cpp:28:   instantiated from here
prog.cpp:10: error: dependent-name ‘std::list::iterator’ is parsed as a non-type,   but instantiation yields a type
prog.cpp:10: note: say ‘typename std::list::iterator’ if a type is meant
Community
  • 1
  • 1
  • I think it's a very bad idea to use the phrase "clearing a pointer", because it means nothing and may be catastrophically misleading. The correct concept is "deleting a dynamically allocated object (by calling `delete` on a pointer to that object)". – Kerrek SB Sep 13 '12 at 14:36
  • @KerrekSB, yeap! you are right. I will change that. – Wanderley Guimarães Sep 13 '12 at 15:16

4 Answers4

2

needs typename, thus:

typename list<ARG>::iterator it;

This is not the only thing wrong with the code though. Why are you casting to int before deleting? That is a bad bad bug when it is not a collection of pointers to ints. You should delete through the proper pointer type if you are going to do it this way.

In addition you could put in a partial-specialisation since ARG has to be a pointer type.

C++ FAQ about templates. I would suggest reading all of that.

Also refer to the FAQ about why casts are evil, and it might also explain what happens with delete.

CashCow
  • 29,087
  • 4
  • 53
  • 86
  • Can you explain why? Or give me some link to read about. – Wanderley Guimarães Sep 13 '12 at 14:34
  • I did it cause I change ARG to int* and the code works and when I change back I didnt change all. (Here is my code: http://ideone.com/L6mQY) Should I edit my question cause it isnt about my main question? It was just a typo. – Wanderley Guimarães Sep 13 '12 at 14:37
  • 1
    delete is not the same as free in C, it doesn't just deallocate the memory but also deletes the object underneath. Casting to int* is thus undefined behaviour if your pointer is not an int*. – CashCow Sep 13 '12 at 14:37
1

Dependent names:

typename list<ARG>::iterator it;
Luchian Grigore
  • 236,802
  • 53
  • 428
  • 594
  • That's not an answer to question. Wow, looking at most of your answers lets me assume you are really on the rep-by-quantity rather than rep-by-quality site. Many of your answers also get flak at first (deservedly) and are really only half-thought about. – Sebastian Mach Sep 14 '12 at 13:04
  • @phresnel the answer contains a fix and a link to an explanation. I don't see how this is any different than the others. What else would you have done? – Luchian Grigore Sep 14 '12 at 13:06
  • By fortune (i.e. your arrogant comment on one post, then a wrong and stupid answer in another, contradicting your arrogance) I have only looked at your answer right now. "The others did it, too" is not an argument of intelligent ppl me. But believe me, I also critique and encourage other people. The problem with your post: In itself, it teaches the asker nothing: Q:"What is wrong with this implementation [...]?", A:"Dependent names!", Q:"Dependent names is wrong?!". Generally, answers on SO should be: a) Answers, b) Self contained (don't depend on Link Rot not happen). Your answer is neither. – Sebastian Mach Sep 14 '12 at 13:10
  • @phresnel well then, by all means - downvote & add your own. – Luchian Grigore Sep 14 '12 at 13:12
  • @phresnel & I only said something about the other answers because you seem to be targeting me - which apparently, in your book, is a sign of intelligence. – Luchian Grigore Sep 14 '12 at 13:13
  • At least it is no sign of stupidness if someone just targets you because of a time-lack or lack of will. Next time, he/she will look at another guy/gal, and on average does a certain duty. Note that I don't want to waste my downvotes on you, I prefer giving an explanation (which I did). – Sebastian Mach Sep 14 '12 at 13:23
  • Luchian: "phresnel you're funny" -> Are you now trying to make friend or is it again (unjustified) arrogance? – Sebastian Mach Sep 14 '12 at 13:24
  • @phresnel actually this does answer the question and matches my answer as to why it doesn't compile. I gave a more complete answer to "what is wrong" with the function, not simply why it didn't compile. – CashCow Sep 21 '12 at 11:28
  • @CashCow: But "Dependent Names" is not even a phrase. However, Luchina and me chatted a bit about this and settled our dispute. – Sebastian Mach Sep 21 '12 at 13:17
1

list<ARG>::iterator is a dependent type:

typename list<ARG>::iterator it;

See Where and why do I have to put the "template" and "typename" keywords?

Community
  • 1
  • 1
hmjd
  • 113,589
  • 17
  • 194
  • 245
0

I think your main error is defining the type of var 'it' somewhere. All the compiler errors are garbage after error 10, because the complier tries to finish the for loop with a non-type var.

David D.
  • 367
  • 1
  • 13