Click here to Skip to main content
15,881,089 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I created three files to complete the linear list:
In main.cpp:
C++
#include "stdafx.h"
#include "Seque.h"

int main(int argc, char* argv[])
{
	printf("Hello World!\n");
	int n,number,position1,insert1,position2;
	Seque<int> seque;
	cout<<"Input the number of numbers you want to enter!"<<endl;
	cin>>n;
	seque.Input(n);
	seque.Output();
	cout<<"Input the number you want to search!"<<endl;
	cin>>number;
	seque.SearchItem(number);
	cout<<"Input the position and number you want to insert!"<<endl;
	cin>>position1;
	cin>>insert1;
	seque.InsertItem(position1,insert1);
	seque.Output();
	cout<<"Input the position you want to delete!"<<endl;
	cin>>position2;
	seque.DeleteItem(position2);
	seque.Output();
	
	return 0;
}

In Seque.cpp:
C++
<pre lang="xml">#include "stdafx.h"
#include "Seque.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
template<class T>
Seque<T>::Seque(int sz)
{
    listsize=sz;
    lenth=-1;
    elem= new T[listsize];
    if (elem==NULL)
    {
        cerr<<"error"<<endl;
        exit(1);
    }
}
template<class T>
void Seque<T>::SearchItem(T& x)
{
    for (int i=0;i<listsize;i++)
    {
        if (elem[i]==x)
        {
            cout<<"The number you search is at "<<i<<"th location"<<endl;
        }
        else
            cout<<"Not Found!"<<endl;
    }
}
template<class T>
void Seque<T>::InsertItem(int i, T& x)
{
    if (lenth==listsize-1)
    {
        cout<<"full"<<endl;
    }
    for (int j=lenth;j>i;j--)
    {
        elem[j+1]=elem[j];
    }
    elem[i]=x;
    lenth++;
}
template<class T>
void Seque<T>::DeleteItem(int i)
{
    int x;
    if (i>lenth)
    {
        cout<<"Exceed!"<<endl;
    }
    x=elem[i];
    for (int j=i;j<=lenth;j++)
    {
        elem[j]=elem[j+1];
    }
    lenth--;
    cout<<"The number at where you point is "<<x<<endl;
}
template<class T>
void Seque<T>::Input(int i)
{
    if (i>listsize)
    {
        cout<<"Error!"<<endl;
        break;
    }
    lenth=i;
    cout<<"Input the elements!"<<endl;
    for (int j=0;j<i;j++)
    {
        cout<<"Number"<<j+1<<endl;
        cin>>elem[j];
    }
}
template<class T>
void Seque<T>::Output()
{
    for (int i=0;i<lenth;i++)
    {
        cout<<"Number "<<i+1<<":"<<endl;
        cout<<elem[i]<<endl;
    }
}



In Seque.h:
C++
<pre lang="xml">#if !defined(AFX_SEQUE_H__78600571_2F0D_49E2_B387_EA39796A31A6__INCLUDED_)
#define AFX_SEQUE_H__78600571_2F0D_49E2_B387_EA39796A31A6__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include <iostream.h>
#include <stdlib.h>
#define Defaultsize 100
template <class T>
class Seque
{
private:
    T *elem;
    int lenth;
    int listsize;
protected:
public:
    Seque(int sz=Defaultsize);
    ~Seque(){delete[] elem;}
    void InsertItem(int i, T& x);
    void DeleteItem(int i);
    void SearchItem(T& x);
    void Input(int i);
    void Output();

};
#endif // !defined(AFX_SEQUE_H__78600571_2F0D_49E2_B387_EA39796A31A6__INCLUDED_)



Errors are:

VB
Linking...
3 Main.obj : error LNK2001: unresolved external symbol "public: void __thiscall Seque<int>::DeleteItem(int)" (?DeleteItem@?$Seque@H@@QAEXH@Z)
3 Main.obj : error LNK2001: unresolved external symbol "public: void __thiscall Seque<int>::InsertItem(int,int &)" (?InsertItem@?$Seque@H@@QAEXHAAH@Z)
3 Main.obj : error LNK2001: unresolved external symbol "public: void __thiscall Seque<int>::SearchItem(int &)" (?SearchItem@?$Seque@H@@QAEXAAH@Z)
3 Main.obj : error LNK2001: unresolved external symbol "public: void __thiscall Seque<int>::Output(void)" (?Output@?$Seque@H@@QAEXXZ)
3 Main.obj : error LNK2001: unresolved external symbol "public: void __thiscall Seque<int>::Input(int)" (?Input@?$Seque@H@@QAEXH@Z)
3 Main.obj : error LNK2001: unresolved external symbol "public: __thiscall Seque<int>::Seque<int>(int)" (??0?$Seque@H@@QAE@H@Z)
Debug/3 Main.exe : fatal error LNK1120: 6 unresolved externals
Error executing link.exe.
Posted

templated functions have to be implemented in the header file.

C#
template <class T>
class Seque
{
private:
    T *elem;
    int lenth;
    int listsize;
protected:
public:

    void InsertItem(int i, T& x)
    {
        //...
    }

    void DeleteItem(int i)
    {
        //...
    }

    //...

};


-PaulH
 
Share this answer
 
v2
Comments
Niklas L 26-Feb-11 3:32am    
The definition has to be within the same translation unit as it is used. Not necessarily a header file.
Paul Heil 26-Feb-11 10:20am    
True.
 
Share this answer
 

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