linked list
Hi I am trying to initialise a generic linked list and i am getting with Visual C++ 2005 error c2664 LinkedNode<T>::LinkedNode(const LinkedNode<T> &) : cannot convert paramter from int to LinkedNode<T>&
with T = int
The function is as follows
template<typename T>
void LinkedList<T>::insertFirst(T data)
{
LinkedNode<T> * newNode;
newNode = LinkedNode<T> (data);//this line the problem
........etc
above is on a windows machine
but I am also using GCC on a Linux Box and the same line is giving me
error: no matching function for call to LinkedNode<int>::LinkedNode(int&)
the calling function in main is
insertFirst(temp) where temp is 1 an integer.
If you can help i will be delighted to hear from you
Regards :)
Michael
It sounds like your LinkedNode class does not have a constructor taking a T as an argument. The compiler is complaining that the only other potential match, the copy constructor, won't accept a T parameter.
Are you aware that the C++ standard library comes with a linked list, std::list in #include <list>?
Are you aware that the C++ standard library comes with a linked list, std::list in #include <list>?
So, does your LinkedNode have a constructor that takes a T (or rather const T&)?
VC++ is saying that it only knows about its copy constructor, the other compilers say that they don't know of a constructor accepting T.
VC++ is saying that it only knows about its copy constructor, the other compilers say that they don't know of a constructor accepting T.
Hi thank you both for the swift reply
My LinkedNode is a struct
template <typename T>
struct LinkedNode
{
T data;
LinkedNode *next;
LinkedNode * prev;
};
I dont think a struct needs a constructor (beginner+/ c++ programmer)
I am aware of STL but only have a few examples in the documentation i have.
My LinkedNode is a struct
template <typename T>
struct LinkedNode
{
T data;
LinkedNode *next;
LinkedNode * prev;
};
I dont think a struct needs a constructor (beginner+/ c++ programmer)
I am aware of STL but only have a few examples in the documentation i have.
While it might not need a constructor, you cannot call one without providing it.
Without a constructor, you would need to do this:
Writing a constructor is a good idea, because that way you cannot forget to set one of the pointers.
Without a constructor, you would need to do this:
ListNode<T> *node = new ListNode<T>();node->data = /* some T instance */;node->next = head;node->prev = 0;
Writing a constructor is a good idea, because that way you cannot forget to set one of the pointers.
Quote:I dont think a struct needs a constructor (beginner+/ c++ programmer)Structs and classes are almost the same in C++ (the only difference is the default privileges), so the same rules and guidelines regarding implementation of constructors, destructors, etc. apply to each.
C++ programmers will often use structs rather than classes for POD types or types with all public member data, but this is simply a matter of convenience (and convention); there's no rule that it has to be done this way.
Quote:I am aware of STL but only have a few examples in the documentation i have.Are you saying you're not using the STL (that is, the standard C++ library) because you don't have suitable documentation available? If that is the case, that shouldn't be an obstacle. If you're having trouble finding info via Google, I'm sure someone here could direct you towards some useful resources.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement