注册 登录
编程论坛 C++教室

c++中各种文件之间的包含与组织

specilize 发布于 2011-04-23 00:15, 578 次点击
Query_base是抽象基类,WordQuery,NotQuery和BinaryQuery继承Query_base类,BinaryQuery也是抽象类,AndQuery和OrQuery继承BinaryQuery类,Query是句柄指针类。Query类重载&,~,|,他们分别需要访问AndQuery,OrQuery,NotQuery,在Query中已经分别将要重载的函数设为友元,因为要返回派生类,所以在派生类定义后才能实现重载函数,
要实现的操作为比如:
Query operator&(const Query& first,const Query& second)
{  return new AndQuery(first,second);}//这里以用复制构造函数将类的指针                                       // 转化为Query对象
以下是我的文件组织形式

Query.h中用#include "Query_base.h"

Query_base.h 不用包含

WordQuery.h中
#include "Query_base.h"
#include "Query.h"
class WordQuery:public Query_base{....}
Query::Query(const string& s)
{ return new WordQuery(s);}

NotQuery.h中
#include "Query_base.h"
#include "Query.h"
class NotQuery::public Query_base{....}
Query operator~(const Query& query)
{  return new NotQuery(query);}

BinaryQuery.h中
#include "Query_base.h"

AndQuery.h中
#include "BinaryQuery.h"
#include "Query.h"
class AndQuery:public BinaryQuery{...}
Query operator&(const Query& first,const Query& second)
{  return new AndQuery(first,second);}

OrQuery.h中
#include "BinaryQuery.h"
#include "OrQuery.h"
class OrQuery:public BinaryQuery{....}
Query operator|(const Query& first,const Query& second)
{  return new OrQuery(first,second);}

然后编译链接时提示的错误为
[Linker error] undefined reference to `Query::Query(std::string const&)'  [Linker error] undefined reference to `Query::Query(std::string const&)'
[Linker error] undefined reference to `Query::Query(std::string const&)'
[Linker error] undefined reference to `operator&(Query const&, Query const&)'
[Linker error] undefined reference to `Query::Query(std::string const&)'
[Linker error] undefined reference to `Query::Query(std::string const&)'
[Linker error] undefined reference to `Query::Query(std::string const&)'
[Linker error] undefined reference to `operator&(Query const&, Query const&)'
[Linker error] undefined reference to `operator|(Query const&, Query const&)'
求高手帮我解释下原因,如何解决,详细点,谢谢
可以的话顺便给点编译链接的资料
8 回复
#2
pangding2011-04-23 00:39
你用什么方法编译的?
#3
specilize2011-04-23 06:50
回复 2楼 pangding
全部编译链接
#4
specilize2011-04-23 07:04
写错了
WordQuery.h中
Query::Query(const string& s)
{ return new WordQuery(s);}
改为Query::Query(const string& s):q(new WordQuery(s)),use(new size_t(1)) { }
#5
pangding2011-04-23 09:56
你用的是 vc 吗?
#6
specilize2011-04-23 11:02
回复 5楼 pangding
Dev c++
#7
pangding2011-04-23 12:34
不清楚。一般这种 IDE,不怎么需要用户管编译链接这些东西呀。

才发现你的文件怎么都是 .h 呀。习惯上有一个 .h 就行了。
#8
specilize2011-04-23 17:57
回复 7楼 pangding
难道类比较多时也只有一个.h文件吗,是不是习惯上将基类和派生类放在头一个头文件声明,我看答案都是这么做的
#9
pangding2011-04-24 00:59
哦。我开始还以为你是每个函数一个 .h 呢。

你这就自己试试呗,我看挺容易的,就是个演示程序。本身就是练語法之类的。
1