Error LNK2001

I created a class node and try to create an instance of it but it keeps giving me errors;
//node.h

#ifndef NODE_H
#define NODE_H

template<class nodetype>
class node
{
      public: nodetype getdata();
                  node(nodetype=0);

      private:nodetype data;
                  node<nodetype> *nextptr;
};

#endif

----------------------------------------------------------------------------------------------------------------

//node.cpp
#include"node.h"

template<class nodetype>
nodetype node< nodetype >::getdata()
{ return data;}

template<class nodetype>
node< nodetype >::node(nodetype value)
{
      data=value;
      nextptr=0;
}

----------------------------------------------------------------------------------------------------------------

#include<iostream.h>
#include"node.h"

void main()
{
      node<int> n(5);


}

----------------------------------------------------------------------------------------------------------------

Compiling...
node.cpp
Linking...
main.obj : error LNK2001: unresolved external symbol "public: __thiscall node<int>::node<int>(int)" (??0?$node@H@@QAE@H@Z)
Debug/assignment1.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.

assignment1.exe - 2 error(s), 0 warning(s)
LVL 1
farroodaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jaime OlivaresSoftware ArchitectCommented:
I think this line:

               node(nodetype=0);

must be:
               node(nodetype value=0);

0
farroodaAuthor Commented:
no it didn't work
same error
0
bcladdCommented:
You don't define a constructor for node<T>. Thus the compiler generates only a default constructor.

-bcl
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Jaime OlivaresSoftware ArchitectCommented:
I have tested and didn't have compiler/linker errors.
0
bcladdCommented:
Oops, my bad. I misread the code.
0
farroodaAuthor Commented:
i didn't understand could you explain more plz?
0
allmerCommented:
Are you using visual studio?
Just hit Build...Rebuild
usually does the trick to get rid of that error ;)
Hope it helps,
Jens
0
farroodaAuthor Commented:
well i am having a linker errors
my compiler is microsoft vc++ 6 service pack 5
should i include something in the project link settings?
0
farroodaAuthor Commented:
same error:(
0
Jaime OlivaresSoftware ArchitectCommented:
Maybe your makefile is not correct. I guess your are compiling all files needed but not linking all together.
0
mnashadkaCommented:
The problem is the template definitions have to be put in the header file.  They can't be in a separate cpp file, because then the preprocessor that expands the template doesn't get to see the actual definition.  Move the node template stuff from the cpp file to the header file, and I'll bet that it will work for you.  Hope this helps.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
farroodaAuthor Commented:
the funny think is if i write
node<int> n(5);
or
node<int> n;

I get an error but when I write node<int> n();
I dont get an error
but when i try to do n.getdata();
it gives me an error:

error C2228: left of '.getdata' must have class/struct/union type


0
Jaime OlivaresSoftware ArchitectCommented:
I agree, that's why I did not compiler errors, because I was lazy and joined all in one file.
0
farroodaAuthor Commented:
thanks mnashadka everything is fine now
0
mnashadkaCommented:
farrooda, I think the error with node<int> n(); is because the compiler is interpreting that as a forward declaration for a function n() that returns type node<int>.  Then, when you call n.getdata(), it has problems since n is a function.

Anyway, I'm glad that I could help.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.