请大家帮我看一看link的问题

freepl 2004-09-08 04:35:27
dfs.cc 功能实现 dfs.h 数据结构的声明 dfs-tst.cc 主函数
在window下面nmake,编译通过了,但link却发生错误。请各位帮我看看。谢谢啦哈。
源文件分别是:
/*
* filename: dfs.h
* src dir: d:\vc71\boost_1_31_0\work\depth_first_search\v0.1
* function: data struct declaration
* demonstrate how to create a dfs_time_visitor
* */
#include <queue>
#include <vector>
#include <algorithm>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/graph_utility.hpp>
#include <boost/graph/depth_first_search.hpp>
#include <boost/pending/integer_range.hpp>
#include <boost/pending/indirect_cmp.hpp>
#define N 13

using namespace std;
using namespace boost;

/* the name dfs_time_visitor is not accurate cos I have put the examine_edge() and tree_edge() in the visitor */
template <typename TimeMap>
class dfs_timer_visitor:public default_dfs_visitor{
typedef typename property_traits<TimeMap>::value_type T;
public:
dfs_timer_visitor(TimeMap timeMap_, T time_):timeMap(timeMap_),time(time_){}
template<typename Vertex, typename Graph>
void discover_vertex(Vertex v, const Graph& g);
template<typename Edge, typename Graph>
void examine_edge(Edge e, const Graph& g);
template <typename Edge, typename Graph>
void tree_edge(Edge e, const Graph& g);
private:
TimeMap timeMap;
T time;
};

/*
* filename: dfs.cc
* src dir: d:\vc71\boost_1_31_0\work\depth_first_search\v0.1
* function: implement file
* demonstrate how to create a dfs_time_visitor
* */
#include "dfs.h"

template<typename TimeMap>
template<typename Vertex, typename Graph>
void dfs_timer_visitor<TimeMap>::discover_vertex(Vertex v, const Graph& g){
put(timeMap, v, time++);
}

template<typename TimeMap>
template<typename Edge, typename Graph>
void dfs_timer_visitor<TimeMap>::examine_edge(Edge e, const Graph& g){
std::cout << source(e, g) << " --> " << target(e, g) << std::endl;
}
template<typename TimeMap>
template<typename Edge, typename Graph>
void dfs_timer_visitor<TimeMap>::tree_edge(Edge e, const Graph& g){
std::cout << source(e, g) << " --> " << target(e, g) << std::endl;
}

/*
* filename: dfs_tst.cc
* src dir: d:\vc71\boost_1_31_0\work\depth_first_search\v0.1
* function: vois main() demonstrate how to create a dfs_time_visitor
* */
#include "dfs.h"

void main(){
typedef adjacency_list<vecS, vecS, directedS, property<vertex_discover_time_t, std::size_t> > Graph;
typedef std::pair<int, int> E;
/* p149 figure 19.1 */
E edge_array[] = {E(4, 2), E(11, 12), E(4, 11), E(5, 4), E(2, 3), E(12, 9), E(4, 3), E(0, 5),
E(3, 2), E(9, 10), E(3, 5), E(6, 4), E(0, 6), E(9, 11), E(7, 8), E(6, 9),
E(0, 1), E(8, 9), E(8, 7), E(7, 6), E(2, 0), E(10, 12)};
/* create a graph */
Graph g(N);
for(std::size_t i = 0; i < sizeof(edge_array)/sizeof(E); ++i){
add_edge(edge_array[i].first, edge_array[i].second, g);
}
print_graph(g);
/* create a dfs_timer_visitor */
typedef property_map<Graph, vertex_discover_time_t>::type t_map_t;
t_map_t t_map = get(vertex_discover_time, g);
typedef property_traits<t_map_t>::value_type T;
T t = 0;
dfs_timer_visitor<t_map_t> vis(t_map, t);
/* depth_first_search */
depth_first_search(g, boost::visitor(vis));
std::vector<T> v(num_vertices(g));

graph_traits<Graph>::vertex_iterator vi, vi_end;
// for(int i = 0,boost::tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi, ++i)
// v[i] = t_map[*vi];
int ii = 0;
for(boost::tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi, ++ii)
v[ii] = t_map[*vi];
integer_range<std::size_t> range(0, N);
std::vector<T> discover_order(N);
std::copy(range.begin(), range.end(), discover_order.begin());
std::sort(discover_order.begin(), discover_order.end(), indirect_cmp<T*, std::less<T> >(&v[0]));
for(int i = 0;i < N; ++i)
cout << discover_order[i] << " ";
}

makefile文件时:
all:dfs.obj dfs_tst.obj
cl dfs.obj dfs_tst.obj /o dfs_tst.exe
dfs.obj:dfs.cc dfs.h
cl /c dfs.cc
dfs_tst.obj:dfs_tst.cc
cl /c dfs_tst.cc
clean:
del *.obj *.exe
我是用vc71编译的。.obj都生成了,但link的时候不对。
...全文
100 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
qwertasdfg123 2004-09-08
  • 打赏
  • 举报
回复
如果在main文件中包含文件dsc.cc也可以的。
如:
#include "dfs.cc"

void main(){......}
积木 2004-09-08
  • 打赏
  • 举报
回复
在C++ templates 这本书里面提到了一个导出模板的模式
但是据说现在支持这种模式的只有一家公司~
主流使用的编译器都还没有支持,所以暂时现在还没有什么办法去解决,只好先这么忍着了
还是这本书,里面提到了一个方法很值得借鉴

首先定义一个
dfs_def.h
然后将模板的定义放进去
再定义一个
dfs.h
这么一个文件,将实现文件放进去并在头里加上#include"dfs_def.h"
这么一句话,然后你在使用的时候直接#include"dfs.h"
就可以了~。
freepl 2004-09-08
  • 打赏
  • 举报
回复
to zhangyu666:
对,dfs_tst.cc是主文件,我前面的中文解释dfs-tst.cc是大错了的, 应该为dfs_tst.cc,hehe
freepl 2004-09-08
  • 打赏
  • 举报
回复
懂了,谢谢三井了哈。
我以前一直是是像你说的那样做的。
这次只是想试试更规范的方法,(声明和实现分开)就出现这个问题。
难道就没有方法解决吗?
飞在天空的鱼 2004-09-08
  • 打赏
  • 举报
回复
dfs-tst.cc这个是主件名吗,你试试把文件名改成

dfs_tst.cc在编译一下

freepl 2004-09-08
  • 打赏
  • 举报
回复
dfs_tst.exe :fatal error link1120 : 3 unresoveled externals
积木 2004-09-08
  • 打赏
  • 举报
回复
你用的是模板?把模板的声明和定义都放到一个.h文件中去就没有问题了~
因为在编译的时候,模板的定义根本就没有被实例化,因此链接程序找不到目标代码可以连接
因此就报错了
同桌老王 2004-09-08
  • 打赏
  • 举报
回复
link时啥错误?

24,854

社区成员

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

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