LNK2019: 无法解析的外部符号

pencil240 2008-11-02 10:27:52
//---------------list.h
#pragma once
#include "stdafx.h"
#include <iostream>
using namespace std;
template <class T>
class LinearList
{
public:
LinearList(int len=10);
~LinearList();
bool IsEmpty() const;
int Length() const;
bool Find(int k,const T& x)const;
int Search(const T& x) const;
LinearList<T>& Delete(int k,T& x) ;
LinearList<T>& Insert(int k,const T& x);
void Output(ostream& out) const;
friend ostream& operator <<(ostream& out,const LinearList<T>& x);

private:
int length;
T*element;
int MaxSize;
};
//----------list.cpp
#include "stdafx.h"
#include"list.h"

template<class T> LinearList<T>::LinearList(int len)
{
MaxSize=len;
element=new T[len];
length=0;
}
template<class T> LinearList<T>::~LinearList()
{
delete[]element;
}
template<class T>
bool LinearList<T>::IsEmpty() const
{
return length==0;
}
template<class T>
int LinearList<T>::Length() const
{
return length;
}
template<class T>
bool LinearList<T>::Find(int k,const T &x) const
{
if(k<=0||k>length)
{
cout<<"none !"<<endl;
return false;
}
else
{
x=element[k-1];
return true;
}
}
template<class T>
int LinearList<T>::Search(const T &x) const
{
int i;
for(i=0;i<length&&x!=element[i];i++);
if(i==length)
return 0;
else
return ++i;

}
template<class T>
LinearList<T>& LinearList<T>::Delete(int k, T &x)
{
if(Find(k,x))
{
for(int i=k;i<length;i++)
element[i-1]=element[i];
length--;
return *this;
}
else
cout<<"error !"<<endl;
}
template<class T>
LinearList<T>& LinearList<T>::Insert(int k, const T &x)
{
if(k<1||k>length)
cout<<"error !"<<endl;
if(length==MaxSize)
cout<<"full !"<<endl;
for(int i=length-1;i>=k-1;i--)
element[i+1]=element[i];
element[k-1]=x;
length++;
return*this;
}
template<class T>
void LinearList<T>::Output(ostream &out) const
{
for(int i=0;i<length;i++)
out<<element[i]<<" ";
}
template<class T>
ostream& operator <<(ostream& out,const LinearList<T>& x)
{
x.Output(out);
return out;
}

//-------------------------- Win32_DS3_1.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include "list.h"

int _tmain(int argc, _TCHAR* argv[])
{
LinearList<int>L(5);
cout << "Length = " << L.Length() << endl;
cout << "IsEmpty = " << L.IsEmpty() <<endl;
L.Insert( 0 , 2 ).Insert ( 1 , 6 ) ;
cout << "List is " << L <<endl;
cout << "IsEmpty = " << L.IsEmpty() << endl;
int z;
L .Find ( 1 , z ) ;
cout << "First element is " << z << endl;
cout << "Length = " << L.Length() << endl;
L .Delete( 1 , z ) ;
cout << "Deleted element is " << z << endl;
cout << "List is " << L << endl;
return 0;
}
程序调试 老是出这个问题 找不着原因 求高手解答~~~~
1>Win32_DS3_1.obj : error LNK2019: 无法解析的外部符号 "public: __thiscall LinearList<int>::~LinearList<int>(void)" (??1?$LinearList@H@@QAE@XZ),该符号在函数 _wmain 中被引用
1>Win32_DS3_1.obj : error LNK2019: 无法解析的外部符号 "public: class LinearList<int> & __thiscall LinearList<int>::Delete(int,int &)" (?Delete@?$LinearList@H@@QAEAAV1@HAAH@Z),该符号在函数 _wmain 中被引用
1>Win32_DS3_1.obj : error LNK2019: 无法解析的外部符号 "public: bool __thiscall LinearList<int>::Find(int,int const &)const " (?Find@?$LinearList@H@@QBE_NHABH@Z),该符号在函数 _wmain 中被引用
1>Win32_DS3_1.obj : error LNK2019: 无法解析的外部符号 "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,class LinearList<int> const &)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@ABV?$LinearList@H@@@Z),该符号在函数 _wmain 中被引用
1>Win32_DS3_1.obj : error LNK2019: 无法解析的外部符号 "public: class LinearList<int> & __thiscall LinearList<int>::Insert(int,int const &)" (?Insert@?$LinearList@H@@QAEAAV1@HABH@Z),该符号在函数 _wmain 中被引用
1>Win32_DS3_1.obj : error LNK2019: 无法解析的外部符号 "public: bool __thiscall LinearList<int>::IsEmpty(void)const " (?IsEmpty@?$LinearList@H@@QBE_NXZ),该符号在函数 _wmain 中被引用
1>Win32_DS3_1.obj : error LNK2019: 无法解析的外部符号 "public: int __thiscall LinearList<int>::Length(void)const " (?Length@?$LinearList@H@@QBEHXZ),该符号在函数 _wmain 中被引用
1>Win32_DS3_1.obj : error LNK2019: 无法解析的外部符号 "public: __thiscall LinearList<int>::LinearList<int>(int)" (??0?$LinearList@H@@QAE@H@Z),该符号在函数 _wmain 中被引用
...全文
248 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
jia_xiaoxin 2008-11-03
  • 打赏
  • 举报
回复
楼上的老大们都解释了:
模板的实现需要放在头文件里
fallening 2008-11-03
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 pencil240 的回复:]
谢谢 部分问题解决 上面还个问题没 解决:
template <class T>
ostream& operator < <(ostream& out,const LinearList <T>& x)
{
x.Output(out);
return out;
}

这句编译的时候出现这个问题:
Win32_DS3_1.obj : error LNK2019: 无法解析的外部符号 "class std::basic_ostream <char,struct std::char_traits <char> > & __cdecl operator < <(class std::basic_ostream <char,struct std::char_traits <char> > &,clas…
[/Quote]
这个问题看楼下的了,我也碰到过,没搞定
pencil240 2008-11-03
  • 打赏
  • 举报
回复
谢谢 部分问题解决 上面还个问题没 解决:
template<class T>
ostream& operator <<(ostream& out,const LinearList<T>& x)
{
x.Output(out);
return out;
}

这句编译的时候出现这个问题:
Win32_DS3_1.obj : error LNK2019: 无法解析的外部符号 "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,class LinearList<int> const &)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@ABV?$LinearList@H@@@Z),该符号在函数 _wmain 中被引用

我查阅了别人的文章,上面说是因为ostream& 的原因,应该用什么 basic_ostream 谁能解释的清楚点么?
帅得不敢出门 2008-11-02
  • 打赏
  • 举报
回复
.......飞雪不厚道 解释也要楼下的
大部分编译器不支持模板分离编译模式
具体解释看楼下:
帅得不敢出门 2008-11-02
  • 打赏
  • 举报
回复
.......飞雪不厚道 解释也要楼下的
大部分编译器不支持模板分离编译模式
具体解释看楼下:
fallening 2008-11-02
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 baihacker 的回复:]
模板的实现请放在头文件里.谢谢.
等楼下的解释原因
[/Quote]
加上export在template之前,然后用intel C++ 10.1编译器就可以了

当初只是无聊之中安装的这个编译器,然后又是无意之中发现它支持逆天的export
baihacker 2008-11-02
  • 打赏
  • 举报
回复
模板的实现请放在头文件里.谢谢.
等楼下的解释原因

65,211

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧