Click here to Skip to main content
15,907,392 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
--------------------------------------Here are my codes----------------------------------------
C++
#ifndef _LIST_H_
#define _LIST_H_
template <typename t="">
struct Num {
    T num;
    Num *next;
};
template <typename t="">
class List {
private:
    Num<t>* pHead;
public:
    List();
    int count();
    void create(T *a, int n);
    void list_();
    void add(T key, int n);
    void Swapat(int a, int b);
    void Swapnum(T num1, T num2);
    int& at(int n);
    void Sort();
    void del(int n);
    void delTheSame();

};
#endif


-------------------And, some of the member functions are as following:-----------------------
C++
template <typename t="">
List<t>::List() {
    pHead = NULL;
}
template <typename t="">
void List<t>::create(T *a, int n) {
    Num<t> *pTail = NULL;
    for (int i = 0; i < n; ++i) {
        Num<t> *t = new Num<t>;
        t->num = a[i];
        t->next = NULL;
        if (pHead == NULL) {
            pHead = t;
            pTail = pHead;
        }
        else {
            pTail->next = t;
            pTail = t;
        }
    }
}
template <typename t="">
void List<t>::list_() {
    Num<t> *t = pHead;
    while (t != NULL) {
        cout << t->num << " ";
        t = t->next;
    }
    cout << endl;
}

----------------------------------But, problems came-------------------------------------------
Severity Code Description Project File Line
Error LNK1120 3 unresolved externals Test C:\Users\Sar.Kerson\Desktop\Test1\Test\Debug\Test.exe 1
Severity Code Description Project File Line
Error LNK2019 unresolved external symbol "public: __thiscall List::List(void)" (??0?$List@H@@QAE@XZ) referenced in function main Test C:\Users\Sar.Kerson\Desktop\Test1\Test\Test\main.obj 1
Severity Code Description Project File Line
Error LNK2019 unresolved external symbol "public: void __thiscall List::create(int *,int)" (?create@?$List@H@@QAEXPAHH@Z) referenced in function _main Test C:\Users\Sar.Kerson\Desktop\Test1\Test\Test\main.obj 1
Severity Code Description Project File Line
Error LNK2019 unresolved external symbol "public: void __thiscall List::list(void)" (?list_@?$List@H@@QAEXXZ) referenced in function _main Test C:\Users\Sar.Kerson\Desktop\Test1\Test\Test\main.obj 1

-------------------------------------Please help-----------------------------------------------

What I have tried:

if I don't use the template, I can run.
I don't know how to solve the problem.
Could you please tell me how to do this.
Posted
Updated 2-Jul-16 4:20am
v3
Comments
Philippe Mori 2-Jul-16 9:51am    
Even if your question is valid and might worth 4 points, because it is not properly formatted, it won't worth much more than 2 points.

Your template code must be in header file to ensure that it is properly instantiated with the appropriate type. Otherwise, you would get link errors.

This is how template works in C++. If you want to understand why, then read appropriate documentation.

To make it simple, essentially the template definition must be visible in the current compilation unit (that is the CPP file under compilation) so that it can be expanded. This is somewhat similar to macro expansion.

ODR (One definition rule) specify that each definition as seen by different compilation units must matches...

If you look at some standard headers like <vector>, you would see that most template code is in header files.
 
Share this answer
 
Comments
Sar Kerson 2-Jul-16 21:43pm    
Oh thanks to your suggestion, I made it this morning. I got rid of the template Strut, then combined the definition of the class and the definition of the member functions together and it works!
I'am sorry but I don't know why it cannot show my codes completely.
every member function is like this:
template "typename T"
List"T":: List() {
.............
}
 
Share this answer
 
v2
Comments
Philippe Mori 2-Jul-16 9:41am    
Use a code block when formatting code. Above the editor, there is a toolbar and when you paste something in the editor, it ask you what format to use with a preview. It could not be easier than that. If something goes wrong, then use Improve solution button and fix any mistake or improper formatting.
Sar Kerson 2-Jul-16 21:44pm    
Thank you so much. I've tried it several times and I came to know how to do this.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900