Click here to Skip to main content
15,892,480 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I have worked out one sequence linearlist structure. However, when I try to run it today, it generates some link errors. Please help me with it.

There are three files in the projects:
Main.cpp
Seque.cpp
Seque.h

The contents are:
C++
In Main.cpp:

#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:
// Seque.cpp: implementation of the Seque class.
//
//////////////////////////////////////////////////////////////////////

#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:

// Seque.h: interface for the Seque class.
//
//////////////////////////////////////////////////////////////////////

#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_)
</t></class></t></class></t></class></t></class></t></class></t></class>

Thank you for helping me!
Posted
Updated 24-Feb-11 21:40pm
v2
Comments
Sandeep Mewara 25-Feb-11 3:40am    
Next time onwards, use PRE tags to format code part.
zhaoyilong1 25-Feb-11 4:06am    
Yes,sorry about this time.
Sandeep Mewara 25-Feb-11 4:14am    
Thats ok. :)
LaxmikantYadav 25-Feb-11 3:44am    
Please post link error.
zhaoyilong1 25-Feb-11 4:05am    
3 ???.obj : error LNK2001: unresolved external symbol "public: void __thiscall Seque<int>::DeleteItem(int)" (?DeleteItem@?$Seque@H@@QAEXH@Z)
3 ???.obj : error LNK2001: unresolved external symbol "public: void __thiscall Seque<int>::InsertItem(int,int &)" (?InsertItem@?$Seque@H@@QAEXHAAH@Z)
3 ???.obj : error LNK2001: unresolved external symbol "public: void __thiscall Seque<int>::SearchItem(int &)" (?SearchItem@?$Seque@H@@QAEXAAH@Z)
3 ???.obj : error LNK2001: unresolved external symbol "public: void __thiscall Seque<int>::Output(void)" (?Output@?$Seque@H@@QAEXXZ)
3 ???.obj : error LNK2001: unresolved external symbol "public: void __thiscall Seque<int>::Input(int)" (?Input@?$Seque@H@@QAEXH@Z)
3 ???.obj : error LNK2001: unresolved external symbol "public: __thiscall Seque<int>::Seque<int>(int)" (??0?$Seque@H@@QAE@H@Z)
Debug/3 ???.exe : fatal error LNK1120: 6 unresolved externals
Error executing link.exe.

When defining a function as a member of a templated class, it is necessary to define it as a templated function.

C++
template class <a_type> void a_class<a_type>::a_function(){...}


I believe your prototypes are incorrect (although I'm noticing pasting template tags in here causes display issues, so it may have been a copy and paste problem)... hence the linker never found a matching template...

Plus you probably need to define template class Seque<int>; in the Seque file.

Decent tutorial: http://www.cprogramming.com/tutorial/templates.html[^]
 
Share this answer
 
v6
Comments
Albert Holguin 25-Feb-11 14:12pm    
wow, pasting code enclosed with <> is almost impossible here! the top line should read...
template<class a_type=""> void a_class<a_type>::a_function(){...}

and bottom line should read...
template class Seque<int>;
AspDotNetDev 25-Feb-11 14:30pm    
There are options below the textbox when you create an answer that allow you to HTML encode or paste text as is. By default, it selects the option to surround your code in PRE tags, and it almost always looks funky when you do that. What I do is select the option to HTML encode when pasting, then I surround the code in PRE tags and indicate the language (in this case, C++). Click "improve solution" on your answer if you would like to see how I modified your answer. Also, I'm not sure I modified it correctly... there was a lot of funky stuff in there. And keep in mind that there is a preview you should review before posting.
Albert Holguin 25-Feb-11 14:45pm    
thanks for the help aspdotnetdev!
Albert Holguin 25-Feb-11 14:48pm    
fixed one last thing, the a_type="" was wrong, thanks for the pointers!
zhaoyilong1 25-Feb-11 21:25pm    
Oh, I believe I make it right in my origin files. I also have the same copy-paste error as you did at first. I think I define template class & function right... But the link errors keep occuring.
Hi,
There are so many problems in your code that I suggest you restart from scratch :(
Your link errors are explained here[^] and in the answers to your Template linear list error, link errors[^] question.

cheers,
AR
 
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