[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 275
  • Last Modified:

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)
0
farrooda
Asked:
farrooda
  • 6
  • 4
  • 2
  • +2
1 Solution
 
Jaime OlivaresCommented:
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Jaime OlivaresCommented:
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 OlivaresCommented:
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
 
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 OlivaresCommented:
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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 6
  • 4
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now