473,396 Members | 1,913 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Trouble with Template classes

Hi. I'm trying to write series of classes using templates and am running into what must be a simple error. I'm a bit of a noob so I apologize in advance if this kind of question is not welcome here.

When I try to run the program below i get the following error during build (using Visual Studio).

Data Structures error LNK2019: unresolved external symbol "public: __thiscall DLNode<int>::DLNode<int>(int)" (??0?$DLNode@H@@QAE@H@Z) referenced in function "public: void __thiscall DLList<int>::addNode(int)" (?addNode@?$DLList@H@@QAEXH@Z)

Note I left the header files out as I don't think the problem is there, but I can include if need be. Any explaination is appreciated:

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include "DLList.cpp"
  3.  
  4. main(){
  5.     DLList<int> intlist;
  6.     intlist.addNode(13);
  7.     printf( "%d", intlist.getFirst() );
  8. }
Expand|Select|Wrap|Line Numbers
  1. #include "DLList.h"
  2.  
  3. template <class Type>
  4. DLList<Type>::DLList(){
  5.     first = 0;
  6.     last = 0;
  7. }
  8.  
  9. template <class Type>
  10. void DLList<Type>::addNode(Type a){
  11.     DLNode<int> intlist(a);
  12.     first = &intlist;
  13. }
  14.  
  15. template <class Type>
  16. Type DLList<Type>::getFirst(){
  17.     return first->getElement();
  18. }
Expand|Select|Wrap|Line Numbers
  1. #include "DLNode.h"
  2.  
  3. template <class Type>
  4. DLNode<Type>::DLNode(Type a){
  5.     data = a;
  6. }
  7.  
  8. template <class Type>
  9. Type DLNode<Type>::getElement(){
  10.     return data;
  11. }
  12. template <class Type>
  13. void DLNode<Type>::setElement(Type a){
  14.     data = a;
  15. }
Jul 19 '07 #1
4 1330
You should include DLList.h, and not .cpp! In fact, you don't need .cpp file for your template implementation: all the template methods figure inside the header, with constructor and all.

So, as soon as you declare a variable in a templated class - like DLNode<int> - then a new class is generated.

You can view templates as "molds" to generate classes. They are not classes by themselves.

Hi. I'm trying to write series of classes using templates and am running into what must be a simple error. I'm a bit of a noob so I apologize in advance if this kind of question is not welcome here.

When I try to run the program below i get the following error during build (using Visual Studio).

Data Structures error LNK2019: unresolved external symbol "public: __thiscall DLNode<int>::DLNode<int>(int)" (??0?$DLNode@H@@QAE@H@Z) referenced in function "public: void __thiscall DLList<int>::addNode(int)" (?addNode@?$DLList@H@@QAEXH@Z)

Note I left the header files out as I don't think the problem is there, but I can include if need be. Any explaination is appreciated:

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include "DLList.cpp"
  3.  
  4. main(){
  5.     DLList<int> intlist;
  6.     intlist.addNode(13);
  7.     printf( "%d", intlist.getFirst() );
  8. }
Expand|Select|Wrap|Line Numbers
  1. #include "DLList.h"
  2.  
  3. template <class Type>
  4. DLList<Type>::DLList(){
  5.     first = 0;
  6.     last = 0;
  7. }
  8.  
  9. template <class Type>
  10. void DLList<Type>::addNode(Type a){
  11.     DLNode<int> intlist(a);
  12.     first = &intlist;
  13. }
  14.  
  15. template <class Type>
  16. Type DLList<Type>::getFirst(){
  17.     return first->getElement();
  18. }
Expand|Select|Wrap|Line Numbers
  1. #include "DLNode.h"
  2.  
  3. template <class Type>
  4. DLNode<Type>::DLNode(Type a){
  5.     data = a;
  6. }
  7.  
  8. template <class Type>
  9. Type DLNode<Type>::getElement(){
  10.     return data;
  11. }
  12. template <class Type>
  13. void DLNode<Type>::setElement(Type a){
  14.     data = a;
  15. }
Jul 19 '07 #2
Thanks for the help. I moved everything into the header file butnow I seem to have a problem accessing data. intlist.getFirst() doesn't return the data. I first thought this was a problem with DLList accessing DLNode methods and tried adding it as a friend to DLNode but got the same results.

Again, this must be something trivial?

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include "DLList.h"
  3.  
  4. main(){
  5.     DLList<int> intlist;
  6.     intlist.addNode(9);
  7.     printf("%d", intlist.getFirst());
  8. }
Expand|Select|Wrap|Line Numbers
  1. #include "DLNode.h"
  2.  
  3. template <class Type>
  4. class DLList{
  5. public:
  6.     DLList(){
  7.     first = 0;
  8.     last = 0;
  9. }
  10.     void addNode(Type a);
  11.     Type getFirst();
  12.  
  13. private:
  14.     DLNode<Type> *first;
  15.     DLNode<Type> *last;
  16. };
  17.  
  18. template <class Type>
  19. void DLList<Type>::addNode(Type a){
  20.     DLNode<int> intlist(a);
  21.     first = &intlist;
  22. }
  23.  
  24. template <class Type>
  25. Type DLList<Type>::getFirst(){
  26.     return first->getElement();
  27. }
Expand|Select|Wrap|Line Numbers
  1. template <class Type> class DLList;
  2.  
  3. template <class Type> class DLNode {
  4.     friend class DLList<Type>;
  5.  
  6. public:
  7.     DLNode(Type a){data = a;}
  8.     Type getElement();
  9.     void setElement(Type a);
  10.  
  11. private:
  12.     Type data;
  13.     DLNode *prevNode;
  14.     DLNode *nextNode;
  15. };
  16.  
  17. template <class Type>
  18.     Type DLNode<Type>::getElement(){
  19.         return data;
  20.     }
  21. template <class Type>
  22.     void DLNode<Type>::setElement(Type a){
  23.         data = a;
  24.     }
Jul 19 '07 #3
Ok, I did some more digging and it is not a problem with the templates it is a problem with the classes. I assume "first" has gone out of scope after the addNode method??? (note I took out the templates stuff for readability)


Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include "DLListint.h"
  3.  
  4. main(){
  5.     DLListint intlist;
  6.     intlist.addNode(9);
  7.     printf("%d", intlist.getFirst());
  8.     getchar();
  9. }
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.  
  3. class DLNodeint {
  4.     friend class DLListint;
  5.  
  6. public:
  7.     DLNodeint(int a){data = a;}
  8.     int getElement();
  9.     void setElement(int a);
  10.  
  11. private:
  12.     int data;
  13.     DLNodeint *prevNode;
  14.     DLNodeint *nextNode;
  15. };
  16.  
  17.     int DLNodeint::getElement(){
  18.         return data;
  19.     }
  20.     void DLNodeint::setElement(int a){
  21.         data = a;
  22.     }
  23.  
  24. class DLListint{
  25. public:
  26.     DLListint(){
  27.     first = 0;
  28.     last = 0;
  29. }
  30.     void addNode(int a);
  31.     int getFirst();
  32.  
  33. private:
  34.     DLNodeint *first;
  35.     DLNodeint *last;
  36. };
  37.  
  38. void DLListint::addNode(int a){
  39.     DLNodeint intlist(a);
  40.     first = &intlist;
  41.     printf("%d\n", a); // works
  42.     printf("%d\n", intlist.getElement()); // works
  43.     printf("%d\n", first->getElement()); // works
  44. }
  45.  
  46. int DLListint::getFirst(){
  47.     printf("%d\n", first->getElement()); // wrong
  48.     return first->getElement();
  49. }
Jul 19 '07 #4
Problem solved: DLNode<int> *intlist = new DLNode<int>(a);


DOH...
Jul 19 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

21
by: Sebastian Faust | last post by:
Hi, is a construction like the following possible: template<class view_model> class template_clase { protected: template_clase() {} virtual ~template_clase() {}
1
by: Oplec | last post by:
Hi, I'm learning C++ as a hobby using The C++ Programming Language : Special Edition by Bjarne Stroustrup. I'm working on chpater 13 exercises that deal with templates. Exercise 13.9 asks for me...
2
by: Jeff | last post by:
/* -------------------------------------------------------------------------- Hello, I was experimenting with class templates and specializing member functions and came across a simple problem...
31
by: nikola | last post by:
Hi all, I was working with a simple function template to find the min of two values. But since I would like the two values to be different (type) I dont know what kind of value (type) it will...
8
by: Tony Johansson | last post by:
Hello Experts! What does this mean actually. If you have a template with a type and non-type template argument, say, like this template<typename T, int a> class Array {. . .}; then A<int,...
12
by: mlimber | last post by:
This is a repost (with slight modifications) from comp.lang.c++.moderated in an effort to get some response. I am using Loki's Factory as presented in _Modern C++ Design_ for message passing in...
7
by: mathieu | last post by:
Hello, I did read the FAQ on template(*), since I could not find an answer to my current issue I am posting here. I have tried to summarize my issue in the following code (**). Basically I am...
45
by: charles.lobo | last post by:
Hi, I have recently begun using templates in C++ and have found it to be quite useful. However, hearing stories of code bloat and assorted problems I decided to write a couple of small programs...
7
by: Amu | last post by:
Hi i am stuck up in a part of project where i have to inherit the VC++ template classes into C#.net. Please provide me the solution for this .
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.