无法解析的外部符号 "class std::basic_ostream .........在线等!

dazhuaye 2008-04-28 09:02:37
#ifndef QUEUE6_H
#define QUEUE6_H

#include<iostream>
using std::cerr;
using std::cout;
using std::ostream;

#include <cstdlib>
using std::exit;

// QueueItem is now a nested type of the template Queue

template <class Type>
class Queue {
public:
Queue() : front( 0 ), back ( 0 ) { }
~Queue();

Type remove();
void add( const Type & );
bool is_empty() const {
return front == 0;
}

friend ostream& operator<<( ostream &, const Queue<Type> & );
private:
class QueueItem {
QueueItem( const Type &t )
: item(t), next(0) { }

friend class Queue<Type>;
friend ostream& operator<<( ostream &, const QueueItem & );

Type item;
QueueItem *next;

void *operator new( size_t );
void operator delete( void *, size_t );

static QueueItem *free_list;
static const unsigned QueueItem_chunk;
};

QueueItem *front;
QueueItem *back;
};

#endif

----------------Queue6.cpp---------------
template <class Type>
ostream& operator<<( ostream &os, const Queue<Type> &q )
{
os << "< ";
QueueItem<Type> *p;
for ( p = q.front; p; p = p->next )
os << *p << " ";
os << " >";
return os;
}

template <class Type>
ostream& operator<<( ostream &os, const typename Queue<Type>::QueueItem &qi )
{
os << qi.item;
return os;
}

#include<iostream>
#include "Queue6.h"
#include "Queue6.cpp"
using std::cout;
using std::endl;

int main() {
Queue<int> qi;
cout << qi << endl;

int ival;
for ( ival = 0; ival < 10; ++ival )
qi.add( ival );
cout << qi << endl;

int err_cnt = 0;
for ( ival = 0; ival < 10; ++ival ) {
int qval = qi.remove();
if ( ival != qval ) err_cnt++;
}

cout << qi << endl;
if ( !err_cnt )
cout << "!! queue executed ok\n";
else cout << "?? queue errors: " << err_cnt << endl;
return 0;
}

error:
Main.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 Queue<int> const &)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@ABV?$Queue@H@@@Z),该符号在函数 _main 中被引用
D:\MyProgamingCode\My-VS08\P16-T\Debug\P16-T.exe : fatal error LNK1120: 1 个无法解析的外部命令

怎么解决???
...全文
1773 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
ouzhf 2010-11-04
  • 打赏
  • 举报
回复
在VS2005中,将Queue类中的友元函数声明如下试试;

//friend ostream& operator<<( ostream &, const Queue & );
friend ostream& operator<< <Type>( ostream &, const Queue & );

//friend ostream& operator<<( ostream &, const QueueItem & );
friend ostream& operator<< <Type>( ostream &, const QueueItem & );

月深夜 2010-08-21
  • 打赏
  • 举报
回复
template<class T>
class Node
{
public:
T data;
Node<T> *next;

Node()
{
this->next=NULL;
}
Node(T data,Node<T> *next=NULL)
{
this->data=data;
this->next=next;
}
};
月深夜 2010-08-21
  • 打赏
  • 举报
回复
我也是同样的问题 我这是第一次用vs2008 迷茫 琢磨了那么久还是在迷茫。。。
月深夜 2010-08-21
  • 打赏
  • 举报
回复
#include<iostream>
#include"Node.h"
using namespace std;
template<class T>
class SingledLinkedList
{
public:
Node<T> *head;

SingledLinkedList();
SingledLinkedList(T value[],int n);
~SingledLinkedList();
bool isEmpty();
int length();
Node<T>* getNode(int i);
T get(int i);
bool set(int i,T x);
friend ostream& operator<<(ostream& out,SingledLinkedList<T> &list);
Node<T>* insert(int i,T x);
bool remove(int i,T& old);
void clear();
void concat(SingledLinkedList<T>& list);
};

template<class T>
SingledLinkedList<T>::SingledLinkedList()
{
this->head=NULL;
}

template<class T>
SingledLinkedList<T>::~SingledLinkedList()
{
clear();
}

template<class T>
bool SingledLinkedList<T>::isEmpty()
{
return head=NULL;
}

template<class T>
int SingledLinkedList<T>::length()
{
int i=0;
Node<T> *p=head;
while(p!=NULL)
{
i++;
p=p->next;
}
return i;
}

template<class T>
Node<T>* SingledLinkedList<T>::getNode(int i)
{
if(i<0)
return NULL;
int j=0;
Node<T> *p=head;
while(p!=NULL && j<i)
{
j++;
p=p->next;
}
return p;
}
template<class T>
T SingledLinkedList<T>::get(int i)
{
Node<T> *p=getNode(i);
if(p!=NULL)
return p->data;
throw "单链表空或参数i指定元素序号无效";
}template<class T>
bool SingledLinkedList<T>::set(int i,T x)
{
Node<T> *p=getNode(i);
if(p!=NULL)
{
p->data=x;
return true;
}
return false;
}

template<class T>
ostream& operator<<(ostream& out,SingledLinkedList<T>& list)
{
Node<T> *p=list.head;
out<<"(";
while(p!=NULL)
{
out<<p->data;
p=p->next;
if(p!=NULL)
out<<",";
}
out<<")\n";
return out;
}
template<class T>
void SingledLinkedList<T>::clear()
{
Node<T> *p=head;
while(p!=NULL)
{
Node<T> *q=p;
p=p->next;
delete q;
}
head=NULL;
}

template<class T>
Node<T>* SingledLinkedList<T>::insert(int i, T x)
{
Node<T>*q=NULL;
if(head==NULL || i<=0)
{
q=new Node<T>(x,head);
head=q;
}
else
{
int j=0;
Node<T> *p=head;
while(p->next!=NULL && j<i-1)
{
j++;
p->next;
}
q=new Node<T>(x,p->next);
p->next=q;
}
return q;
}

template<class T>
bool SingledLinkedList<T>::remove(int i, T &old)
{
if(head!=NULL && i>=0)
if(i==0)
{
Node<T> *q=head;
old=q->data;
delete q;
head=head->next;
return true;
}
else
{
Node<T> *p=getNode(i-1);
if(p!=NULL && p->next!=NULL)
{
Node<T> *q=p->next;
old=q->data;
p->next=q->next;
delete q;
return true;
}
}
return false;
}

taodm 2008-04-28
  • 打赏
  • 举报
回复
兄弟啊,你还是先using namespace std;
另外,#include "XXX.cpp"是一个不好的编程风格,把那个cpp里的内容全部拷到那个.h里把。
dazhuaye 2008-04-28
  • 打赏
  • 举报
回复
是啊VS2008下!

65,211

社区成员

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

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