Click here to Skip to main content
15,910,123 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a header file and a cpp file.
//////Node.h
	namespace Collections
	{
		/* Node */
		template<typename T>
		class Node
		{
		public:
			T element;
			Node *next;
			
			Node();
			Node(T element);
		};
	} // end namespace Collections


///// Node.cpp
#include "LinkedList.h"

	namespace Collections
	{
		template<typename T>
		Node<t>::Node()
		{
			next = NULL;
		}

		template<typename T>
		Node<t>::Node(T element)
		{
			this->element = element;
			next = NULL;
		}
	} // end namespace Collections


// app.cpp
// omit some "include"......
#include "collections/linkedlist.h"

using namespace std;
using namespace Collections;

int main()
{
	Node<string> *head;
	
	head = new Node<string>();
	head->element = "Nicky";
	head->next = NULL;
	tail = head;
	
	do
	{
		cout << tail->element << endl;
	}
	while(tail = tail->next);

	return 0;
}
</t></t>

then I CANNOT compile the project because of the error:
error LNK2019: cannot solve the external symbol ""public: __thiscall Collections::Node<class>,class std::allocator<char> > >::Node<class>,class std::allocator<char> > >(void)" (??0?$Node@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Collections@@QAE@XZ)"

Can anyone help me? It makes me confusing a lot..

And I CAN compile if I copy the code from CPP file to header file....
Posted
Updated 21-Jun-10 23:22pm
v4

You've already solved the problem :)
Move the code from the .cpp file to the .h, file. The compiler needs to see both the definition and the implementation to generate the correct code.

Remember that when using templates, you're only providing a pattern, not an actual implementation.

Read this for a better explanation.

Hope this helps,
Fredrik
 
Share this answer
 
C++
template<typename t="">

"" is not a type. Replace it with std::string or const char* or some other type, or just skip the default. Your Node class is really generic and should be better off without.

If you want specifics, use a typedef later

typedef Node<string> StringNode;
 
Share this answer
 
Sorry, the editor modified my code.

Actually, it is "template<typename t="">", and it worked if I moved the code from Node.cpp to Node.h.

That is why I confusing...
 
Share this answer
 
Thanks for all your help!! :)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900