Link to home
Start Free TrialLog in
Avatar of farrooda
farrooda

asked on

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)
Avatar of Jaime Olivares
Jaime Olivares
Flag of Peru image

I think this line:

               node(nodetype=0);

must be:
               node(nodetype value=0);

Avatar of farrooda
farrooda

ASKER

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

-bcl
I have tested and didn't have compiler/linker errors.
Oops, my bad. I misread the code.
i didn't understand could you explain more plz?
Are you using visual studio?
Just hit Build...Rebuild
usually does the trick to get rid of that error ;)
Hope it helps,
Jens
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?
same error:(
Maybe your makefile is not correct. I guess your are compiling all files needed but not linking all together.
ASKER CERTIFIED SOLUTION
Avatar of mnashadka
mnashadka

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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


I agree, that's why I did not compiler errors, because I was lazy and joined all in one file.
thanks mnashadka everything is fine now
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.