有关编程的问题..... [英] Question about programming.....
问题描述
我已经计算出一种序列线性表结构.但是,当我今天尝试运行它时,它会生成一些链接错误.请帮帮我.
项目中有三个文件:
Main.cpp
Seque.cpp
Seque.h
内容是:
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:
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!
推荐答案
在将函数定义为模板化类的成员时,有必要将其定义为模板化函数.
When defining a function as a member of a templated class, it is necessary to define it as a templated function.
template class <a_type> void a_class<a_type>::a_function(){...}
我相信您的原型是不正确的(尽管我注意到此处粘贴模板标签会导致显示问题,所以可能是复制和粘贴问题)...因此,链接程序从未找到匹配的模板...
另外,您可能需要在Seque文件中定义模板类Seque< int> ;;
.
体面的教程: http://www.cprogramming.com/tutorial/templates.html [
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[^]
您好,
您的代码中有太多问题,我建议您从头开始重新开始:(
此处 [模板线性列表错误,链接错误 [
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
这篇关于有关编程的问题.....的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!