主题:[讨论]编译通不过----新手请教
我的编译通不过,不能运行,大家帮我看看
我创建了一个:工程->win32 console application
编译有如下错误
Compiling...
linear.cpp
Linking...
LIBCD.lib(crt0.obj) : error LNK2001: unresolved external symbol _main
Debug/网上.exe : fatal error LNK1120: 1 unresolved externals
执行 link.exe 时出错.
网上.exe - 1 error(s), 0 warning(s)
文件如下:
***************
//LinearList.h
********************
//class 声明
#include <iostream>
using namespace std;
template <class T>
class LinearList
{
public: //默认为private
LinearList(int MaxSize = 10);
~LinearList(){delete []element;}
bool IsEmpty()const{return length==0;}
int Length()const{return length;}
bool Find(int k, T &x)const; //返回第K个元素至尽x中
int Search(const T &x)const; //返回x所在的位置
LinearList<T> & Delete(int k, T &x);
LinearList<T> & Insert(int k, const T &x);
void Output(ostream & out)const;
void Output(ostream & out);
private:
int length;
int MaxSize;
T *element;
};
*****************
//LinearList.cpp
********************
#include "LinearList.h"
template <class T>
LinearList<T> :: LinearList(int MaxListSize)
{
MaxSize = MaxListSize;
element = new T[MaxSize];
length = 0;
}
template <class T>
bool LinearList<T>::Find(int k,T &x)const
{ //找到下标为K的元素存放在x中
if(k<1 || k>length) return false;
x=element[k-1];
return true;
}
template <class T>
int LinearList<T>::Search(const T &x)const
{
for(int i=0;i<length;i++)
if(element[i] == x) return ++i; //i表示第几个元素,不是下标
return 0;
}
template <class T>
LinearList<T> & LinearList<T>::Delete(int k, T &x)
{
if(k<1 || k>length) exit(1);//
x=element[k-1];//
for(i=k-1;i<length;i++) element[i]=element[i+1];
length--;
return *this;
}
template <class T>
LinearList<T> & LinearList<T>::Insert(int k, const T &x)
{ //第k个元素之后插入
if(k>MaxSize) exit(1);
for(int i=length-1;i>k;i--) element[i+1]=element[i];
element[i]=x;
length++;
return *this;
}
template <class T>
void LinearList<T>::Output(ostream & out)const
{
for(int i=0;i<length;i++)
out<<element[i]<<endl;
}
template <class T>
ostream & operator<<(ostream & out,const LinearList<T> &x)
{
x.Output(out);
return out;
}
**********************
//UserList.h
***********************
#include <iostream.h>
#include "LinearList.h"
void main()
{
LinearList<int> L(5);
cout<<L.Length()<<L.IsEmpty();
L.Insert(0,2).Insert(1,6);
cout<<L.Length<<L.IsEmpty();
system("pause");
}
我创建了一个:工程->win32 console application
编译有如下错误
Compiling...
linear.cpp
Linking...
LIBCD.lib(crt0.obj) : error LNK2001: unresolved external symbol _main
Debug/网上.exe : fatal error LNK1120: 1 unresolved externals
执行 link.exe 时出错.
网上.exe - 1 error(s), 0 warning(s)
文件如下:
***************
//LinearList.h
********************
//class 声明
#include <iostream>
using namespace std;
template <class T>
class LinearList
{
public: //默认为private
LinearList(int MaxSize = 10);
~LinearList(){delete []element;}
bool IsEmpty()const{return length==0;}
int Length()const{return length;}
bool Find(int k, T &x)const; //返回第K个元素至尽x中
int Search(const T &x)const; //返回x所在的位置
LinearList<T> & Delete(int k, T &x);
LinearList<T> & Insert(int k, const T &x);
void Output(ostream & out)const;
void Output(ostream & out);
private:
int length;
int MaxSize;
T *element;
};
*****************
//LinearList.cpp
********************
#include "LinearList.h"
template <class T>
LinearList<T> :: LinearList(int MaxListSize)
{
MaxSize = MaxListSize;
element = new T[MaxSize];
length = 0;
}
template <class T>
bool LinearList<T>::Find(int k,T &x)const
{ //找到下标为K的元素存放在x中
if(k<1 || k>length) return false;
x=element[k-1];
return true;
}
template <class T>
int LinearList<T>::Search(const T &x)const
{
for(int i=0;i<length;i++)
if(element[i] == x) return ++i; //i表示第几个元素,不是下标
return 0;
}
template <class T>
LinearList<T> & LinearList<T>::Delete(int k, T &x)
{
if(k<1 || k>length) exit(1);//
x=element[k-1];//
for(i=k-1;i<length;i++) element[i]=element[i+1];
length--;
return *this;
}
template <class T>
LinearList<T> & LinearList<T>::Insert(int k, const T &x)
{ //第k个元素之后插入
if(k>MaxSize) exit(1);
for(int i=length-1;i>k;i--) element[i+1]=element[i];
element[i]=x;
length++;
return *this;
}
template <class T>
void LinearList<T>::Output(ostream & out)const
{
for(int i=0;i<length;i++)
out<<element[i]<<endl;
}
template <class T>
ostream & operator<<(ostream & out,const LinearList<T> &x)
{
x.Output(out);
return out;
}
**********************
//UserList.h
***********************
#include <iostream.h>
#include "LinearList.h"
void main()
{
LinearList<int> L(5);
cout<<L.Length()<<L.IsEmpty();
L.Insert(0,2).Insert(1,6);
cout<<L.Length<<L.IsEmpty();
system("pause");
}