{求助}Xcode下使用自己的库Build失败

heyuxuanzee 2010-11-11 03:31:27
这是我写的一个程序,是横向输出二叉树的算法。
其中把二叉树类写到一个库中,.h里放类的定义,.cpp放类的实现。。。

我在Xcode中运行就会出现如下提示:

"BinaryTree<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >::PrintByLine()", referenced from:
_main in main.o
ld: symbol(s) not found
collect2: ld returned 1 exit status



但是如果把类的实现写到.h中,就可以成功运行。。。
如果放在vs2008中,也可以运行。。。

搞不明白为什么在Xcode下,分开放就不可以了。。。求高手指教!谢谢!

正面是我的程序:


#include <iostream>
#include "Binary.h"
#include <string>
using namespace std;



int main() {
// insert code here...
BinaryTree <string> tree("this is a root");
BinaryNode <string> * p,*q;
p=new BinaryNode<string>("root's left");
q=tree.GetRoot();
q -> SetLeft(p);
p=new BinaryNode<string>("root's right");
q -> SetRight(p);

tree.PrintByLine();

return 0;

}



/*
* Binary.h
* tree
*/
#ifndef _BINARY_LAOHYX
#define _BINARY_LAOHYX
#include <iostream>
#include <queue>

using namespace std;

template <class type>
class BinaryNode {
private:
BinaryNode<type> *left,*right;
type data;
public:
BinaryNode():left(NULL),right(NULL){} //二叉树的结点构造函数
BinaryNode(type item,BinaryNode<type> *L=NULL,BinaryNode<type> *R=NULL):data(item),left(L),right(R){} //带参数的构造函数
type GetData() const {return data;} //return this tree's data
~BinaryNode(){}
BinaryNode <type> * GetLeft() const {return left;}
BinaryNode <type> * GetRight() const {return right;}
void SetData (const type & item) {data=item;}
void SetLeft (BinaryNode<type> *L) {left=L;}
void SetRight (BinaryNode<type> *R) {right=R;}
int Size(const BinaryNode<type> *T) const; //返回以T为根的树结点个数
int Height(const BinaryNode<type> *T) const; //返回以T为根的树的高度
};


template <class type>
class BinaryTree {
private:
BinaryNode <type> * root; //the root of a tree
BinaryTree (const BinaryTree<type> &);
void DelTree (const BinaryNode<type> *T);


public:
BinaryTree():root(NULL){}
BinaryTree(const type &value)
{root = new BinaryNode<type> (value);}
int IsEmpty() const {return root==NULL;}
BinaryNode<type> * GetRoot() const {return root;}
void MakeEmpty(){DelTree(root);root=NULL;}
void PrintByLine();
//const BinaryTree <type> &operator =(const BinaryTree<type> &T);
};


#endif





/*
* Binary.cpp
* tree
*
*/
#include "Binary.h"
#include <iostream>
using namespace std;




template <class type>
type Max (const type u, const type v)
{
if (u>v)
return u;
else
return v;
}

template <class type>
int BinaryNode <type> :: Size(const BinaryNode<type> *T)const
{
if (T == NULL)
return 0;
else
return 1+Size(T->left)+Size(T->right);
}

template <class type>
int BinaryNode <type> :: Height(const BinaryNode<type> *T)const
{
if (T == NULL)
return 0;
else
return 1+Max(Size(T->left),Size(T->right));
}
template <class type>
void BinaryTree <type> :: DelTree(const BinaryNode<type> *T)
{
if (T != NULL) {
DelTree(T -> left);
DelTree(T -> right);
delete T;
}
}





/****************************************************
按行进行树的输出
****************************************************/
template <class type>
void BinaryTree <type> :: PrintByLine()
{
queue<BinaryNode<type>*> que;
cout << "Print by line :";
que.push(NULL);
que.push(root);
int layer=0;

while (!que.empty()) {
if (que.front()==NULL)
{
que.push(NULL);
que.pop();
if (que.front()==NULL) {
break;
}
layer++;
cout << "\nLayer " << layer << ":\n";
}
cout << que.front() -> GetData() << " ";
que.push(que.front() -> GetLeft());
que.push(que.front() -> GetRight());
que.pop();
}
}









问题就是出在最后一个函数上。。。。求高手解释xcode和vs的区别。。。。
...全文
88 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
heyuxuanzee 2010-11-11
  • 打赏
  • 举报
回复
我做的库文件就放在同一目录下。。。
其实把库分成2个文件就容易出错这种事情已经困扰我很久了。。。
yashuwa0622cvte 2010-11-11
  • 打赏
  • 举报
回复
使用的库有没有放到正确的路径下面?

24,855

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 工具平台和程序库
社区管理员
  • 工具平台和程序库社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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