Jump to content
  • Advertisement
Sign in to follow this  
Michael Cloran

linked list

This topic is 3411 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
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>?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.


Share this post


Link to post
Share on other sites
While it might not need a constructor, you cannot call one without providing it.

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!