您的位置:程序门 -> c/c++ -> c++ 语言



c++


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


c++[已结贴,结贴人:hastings]
发表于:2007-08-31 12:10:20 楼主
我按照c++primer第四版中文版543页的例题照样编了个类模版,
总共有三个文件:
1.类模版头文件queue.h
2.类模版源文件queue.cpp
3.测试用的主文件text.cpp(即有那个   main()函数)
问题是:三个文件的头文件我弄不来
例:
//queue.h文件简略如下:
#ifndef   queue_h
#define   queue_h
。。。。。。//类定义
#include   "queue.cpp "
#endif

//queue.cpp文件简略如下:
。。。。。。//成员定义(没有#include   "queue.h "这句)

//text.cpp文件简略如下:
#include   "queue.h "
int   main()
{......
return   0;}
但是,这样错误很多。。。怎么回事???

另一种搞法就没错误,如下:
//queue.h文件简略如下:
#ifndef   queue_h
#define   queue_h
。。。。。。//类定义
//这里少了那句#include   "queue.cpp "
#endif

//queue.cpp文件简略如下:
#include   "queue.h "//这句是加上去的
。。。。。。//成员定义

//text.cpp文件简略如下:
#include   "queue.cpp "//(为啥不是#include   "queue.h "?)
int   main()
{......
return   0;}

我想在用到这个类模板的时候,这样写头文件:#include   "queue.h ",而不是这样写:#include   "queue.cpp "。该怎么弄??
以下为完整的文件:

//queue.h文件如下:
#ifndef   queue_h
#define   queue_h
#include <iostream>
template   <class   type> class   queue;
template   <class   type> class   queueitem
{
queueitem(const   type&   t):item(t),next(0){}
type   item;
queueitem*   next;
friend   class   queue <type> ;
};

template   <class   type> class   queue
{
public:
queue():head(0),tail(0){}
queue(const   queue&   q):head(0),tail(0){copy_elems(q);}
queue&   operator=(const   queue&);
~queue(){destroy();}
type&   front(){if(!empty())   return   head-> item;}
type&   back(){if(!empty())   return   tail-> item;}
const   type&   front()const{if(!empty())   return   head-> item;}
const   type&   back()const{if(!empty())   return   tail-> item;}
void   pushback(const   type&);
void   pushfront(const   type&);
void   popfront();
void   popback();
bool   empty()const{return   head==0;}
void   display();///
int   getcount();///
private:
queueitem <type>   *head,*tail;
void   destroy();
void   copy_elems(const   queue&);
};

//#include   "queue.cpp "
#endif

//queue.cpp文件如下:
#include   "queue.h "
//#include <iostream>
template   <class   type>   void   queue <type> ::destroy()
{
while(!empty())
popfront();
}

template   <class   type>   void   queue <type> ::popfront()
{
if(!empty())
{
queueitem <type> *   p=head;
head=head-> next;
delete   p;
}
}

template   <class   type>   void   queue <type> ::popback()
{
if(!empty())
{
int   n=getcount();
queueitem <type> *   p=head;
if(n> =2)
{
int   i=2;
for(;i <n;p=p-> next,i++);//n=2
tail=p;
p=p-> next;
tail-> next=0;
delete   p;
}
else
{
delete   p;
head=tail=0;
}


}
}

template   <class   type>   void   queue <type> ::pushfront(const   type&   val)
{
queueitem <type> *   p=new   queueitem <type> (val);
if(empty())
head=tail=p;
else
{
queueitem <type> *   p1=head;
head=p;
head-> next=p1;
}
}

template   <class   type>   void   queue <type> ::pushback(const   type&   val)
{
queueitem <type> *   p=new   queueitem <type> (val);
if(empty())
head=tail=p;
else
{
tail-> next=p;
tail=p;
}
}

template   <class   type>   void   queue <type> ::copy_elems(const   queue&   orig)
{
for(queueitem <type> *   p=orig.head;p;p=p-> next)
pushback(p-> item);
}

template   <class   type>   queue <type> &   queue <type> ::operator=(const   queue&   orig)
{
destroy();
for(queueitem <type> *   p=orig.head;p;p=p-> next)
pushback(p-> item);
return   *this;
}

template   <class   type>   void   queue <type> ::display()
{
if(empty())
std::cout < < "容器内没有元素   ^_^。。 " < <endl;
else
{
for(queueitem <type> *   p=head;p;p=p-> next)
std::cout < <p-> item < < "\t ";
std::cout < <std::endl;
}
}

template   <class   type>   int   queue <type> ::getcount()
{ int   i(0);
for(queueitem <type> *   p=head;p;p=p-> next,i++);
return   i;
}

//text.cpp文件如下:
#include   "queue.cpp "
#include <string>
using   std::string;
using   std::cin;
using   std::cout;
using   std::endl;
int   main()
{
string   s;
queue <string>   sq;
cout < < "请输入几个字符串,进行pushback操作: " < <endl;
while(cin> > s)
{
sq.pushback(s);
}
cin.clear();//清状态,使有效;
cout < < "sq=\t ";
sq.display();
queue <string>   sqq(sq);
cout < < "sqq=\t ";
sqq.display();
queue <string>   sqqq;
sqqq=sqq;
cout < < "sqqq=\t ";
sqqq.display();
cout < < "下面是sq.popback()操作: " < <endl;
while(!sq.empty())
{
sq.popback();
sq.display();
}
cout < < "下面是sqq.popfront()操作: " < <endl;
while(!sqq.empty())
{
sqq.popfront();
sqq.display();
}
cout < < "sqqq=sq操作之前的sqqq.getcount()=\t " < <sqqq.getcount() < <endl;
sqqq=sq;
cout < < "sqqq=sq操作之后的sqqq.getcount()=\t " < <sqqq.getcount() < <endl;
cout < < "sqqq=\t ";
sqqq.display();
cout < < "请输入几个整数,进行pushfront操作: " < <endl;
queue <int>   iq;
int   i;
while(cin> > i)
{
iq.pushfront(i);
}
cin.clear();
cout < < "iq=\t ";
iq.display();
cout < < "iq.getcount()=\t " < <iq.getcount() < <endl;
cout < < "iq.front()=\t " < <iq.front() < <endl;
cout < < "iq.back()=\t " < <iq.back() < <endl;
iq.back()=iq.front();
cout < < "iq.back()=iq.front()操作之后\n ";
cout < < "iq=\t ";
iq.display();

return   0;
}
这个模板只支持简单类型的操作,而且front()函数和back()函数在empty的情况下会出错。问题是:头文件怎么个包含法????
发表于:2007-08-31 12:11:401楼 得分:0
佩服楼主
发表于:2007-08-31 12:32:012楼 得分:0
我刚刚注册这个网站,不知道怎么奖励分数。。
还有,怎么查看自己的分数?
发表于:2007-08-31 14:12:283楼 得分:20
看c++   primer关于模板分别编译问题,在模板那一章里面。

//queue.h文件

#ifndef   queue_h
#define   queue_h
#include <iostream>
//......
#include   "queue.cpp "         //这里不能少
#endif


//queue.cpp文件

#ifndef   _queue_cpp
#define   _queue_cpp
#include   "queue.h "
//......

#endif


//main文件
#include   "queue.h "       //ok,现在可以include   "queue.h "
#include <string>
using   std::string;
using   std::cin;
using   std::cout;
using   std::endl;
int   main()
{
发表于:2007-08-31 14:32:574楼 得分:0
呵呵,lz大概是刚学template吧

一般来说,模版类是不能分开写的,即使分开了,在使用的时候也要全部引用起来的。所以,一般我们把实现都放在.h文件里。

如果一定要分开,比较通用的方法是:
再建一个     ???.ini   文件,在这个文件里实现cpp里类似的功能

然后在头文件的末尾加上
#include   "???.ini "

当然,文件扩展名无所谓啦,你要不觉得难看,cpp也没啥的

还有,给分点击 "管理 "
发表于:2007-08-31 22:28:335楼 得分:0
谢谢3楼的jxlczjp77()兄弟,问题已经解决。。


快速检索

最新资讯
热门点击