Solved

Template header... headache...

Posted on 2007-03-25
7
256 Views
Last Modified: 2010-04-01
I have the following problem which I am confused about. So I would like some explanation if possible.

Now, I have 3 cpp files, 2 .h headers.

common_list.h:
template <class ListDat>
class list
{
      private:
            struct item
            {
                  ListDat data;
                  
                  item *next;
            };

            item *node;
            item *current;
      
      public:
            list();
            ~list();
            
            void add(ListDat data);
            bool first_item(ListDat &data);
            bool next_item(ListDat &data);
            //bool reverse();
            bool isEmpty();
            int len();
            void rewind();
};


BigNumber.h:
//-------------Prototypes-------------------------
class BigNumber
{
      private:
            list<int> digits;

      public:
            BigNumber();
            ~BigNumber();

            void Read();
            //void Add(list<int> first, list<int> second, list<int> &result);
            void Add(BigNumber first, BigNumber second, BigNumber &result);
            void Print();
};


ok, so Now in BigNumber.cpp, i have:

#include "common_list.h"
#include "BigNumber.h"

Main.cpp:

#include "common_list.h"
#include "BigNumber.h"


Here is the problem, whenever I tried to compile this, I get all these unresolved names error from the linker.. can anyone tell me what is wrong?

I have tried having all code in one cpp file, and it worked perfectly fine... its only when I have them seperated into their own files and written their own headers that this happens.

Thanks in advanced
0
Comment
Question by:sakuya_su
  • 3
  • 3
7 Comments
 
LVL 10

Author Comment

by:sakuya_su
ID: 18787578
to elaborate further, these are the errors:

Error      1      error LNK2019: unresolved external symbol "public: __thiscall list<int>::list<int>(void)" (??0?$list@H@@QAE@XZ) referenced in function "public: __thiscall BigNumber::BigNumber(void)" (??0BigNumber@@QAE@XZ)      BigNumber.obj

Error      2      error LNK2019: unresolved external symbol "public: __thiscall list<int>::~list<int>(void)" (??1?$list@H@@QAE@XZ) referenced in function "public: __thiscall BigNumber::~BigNumber(void)" (??1BigNumber@@QAE@XZ)      BigNumber.obj

...

basically any of the functions in that template class
0
 
LVL 11

Expert Comment

by:DeepuAbrahamK
ID: 18787591
In your header file add these lines:

#ifndef _COMMON_LIST_H
#define _COMMON_LIST_H
....
your header file contents...
......
#endif
Best Regards,
DeepuAbrahamK
0
 
LVL 10

Author Comment

by:sakuya_su
ID: 18787608
There are 2 header files.

but anyway the problem is still there.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 4

Assisted Solution

by:MacroLand
MacroLand earned 100 total points
ID: 18787680
in BigNumber.h include common_list.h;
in BigNumber.cpp include BigNumber.h
in main function just include #include "BigNumber.h";

besides always use header guards as DeepAbrahamK suggested.

And I think you separate common_list.h and common_list.cpp

so in your common_list.cpp file so your functions should be like;

common_list.cpp;

template <class ListDat> list<ListDat>::list()
{
}

Regards,
0
 
LVL 11

Expert Comment

by:DeepuAbrahamK
ID: 18787688
By the way did you include,

#include <list>
using namespace std;

Best Regards,
DeepuAbrahamK
0
 
LVL 11

Accepted Solution

by:
DeepuAbrahamK earned 400 total points
ID: 18787694
Sorry I have overlooked :( never mind!! You have a seperate list class.
0
 
LVL 10

Author Comment

by:sakuya_su
ID: 18787699
lol I was very dumb >.> there was a <list> lib..... my types were overloaded...... thanks
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now