Generic class error: cannot pass a pointer of the generic type

I have written a simple generic list class called List. The class contains a function called add() that takes a pointer of the generic object. The argument is also default initiated with zero if no argument is passed to the function.
1
2
3
4
template <class T>
void List<T>::add(T *t = 0)
{
}


From the main function, I have this:
1
2
3
4
List<Person*> list;
Person *p = new Person;
list.add();
list.add(p); // generates compile error 

The error the above code generates is:
no mathing function call for call to 'List<Person*>::add(Person*&)'
note: candidates are: void List<T>::add(T*) [with T = Person*]


Why canĀ“t I send the pointer of type Person?
Because it expects a pointer to a pointer to a person.
Your type T is Person*.

So, your function add(T *t) takes a pointer to type T, so a pointer to a Person*, which is a pointer to a pointer to a Person.

You're trying to pass it a pointer to a Person, when it expects a pointer to a pointer to a Person.

Edit: Beaten to the punch by L B!

More Edit: But ahead of Catfish! Some of my shame is taken by the Catfish. Yay! :p
Last edited on
It seems to me that in your case List<Person*> list; would translate to:
1
2
3
void List::add(Person* *t = 0) // see the double asterisks?
{
}


Edit: Well I got beaten by not one, but two people. Disgraceful.
Last edited on
Uhm, ok.

So
1
2
3
4
5
6
7
8
9
10
int main()
{
     List<Person*> list;
     Person *p = new Person;
     list.add(p);
}
void List::add(Person p)
{
// Is p some kind of pointer here?
}

equals to
1
2
3
4
5
6
7
8
9
int main()
{
     List<Person> list;
     Person p;
     list.add(&p);
}
void List::add(Person *p)
{
}

or something like that?
Or am I mistaken?

If I declare the list as:
List<Person*> list;
Is the type T t in the class List a pointer, for example, is the variable p in the below code a pointer:
1
2
3
void List::add(Person p)
{
}
if the list declaration is List<Person*> list;?
Last edited on
Look at your code in the first post, and then read catfish's post.
Topic archived. No new replies allowed.