LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1098|回复: 4

在STL中编译一个Demo出现 undefined reference to `Foo::Foo(int, int)'

[复制链接]
发表于 2006-3-14 14:57:15 | 显示全部楼层 |阅读模式
这是main.cpp文件


  1. ...
  2. int main(void)
  3. {
  4.      ...
  5.      vector<Foo> vec_foo;
  6.      cin >> x >> y >>z;
  7.      Foo Obj1(x, y, z);
  8.      vec_foo.push(Obj1);
  9.      ...
  10. }
复制代码

这是Foo.h头文件

  1. ...
  2. class Foo
  3. {
  4. public:
  5.         Foo(int , int );
  6.         ...
  7. }
复制代码

这是Foo.cpp文件

  1. ...
  2. #include "foo.h"
  3. ...
  4. Foo::Foo(int Px, int Py)
  5. {
  6.       x  =  Px;
  7.       y  =  Py;
  8. ...
  9. }
复制代码

编译如下:

  1. [root@root TEST]# ls
  2. Foo.cpp  Foo.h  main.cpp  test  test6  test.cpp
  3. [root@root TEST]# g++ main.cpp -o main
  4. /tmp/ccFq6Xda.o(.text+0x125): In function `main':
  5. : undefined reference to `Foo::Foo(int, int)'
  6. /tmp/ccFq6Xda.o(.text+0x148): In function `main':
  7. : undefined reference to `Foo::~Foo()'
  8. /tmp/ccFq6Xda.o(.text+0x166): In function `main':
  9. : undefined reference to `Foo::~Foo()'
  10. /tmp/ccFq6Xda.o(.gnu.linkonce.t._ZN3FooC1ERKS_+0x8): In function `Foo::Foo(Foo const&)':
  11. : undefined reference to `vtable for Foo'
  12. /tmp/ccFq6Xda.o(.gnu.linkonce.t._ZNSt6vectorI3FooSaIS0_EE13_M_insert_auxEN9__gnu_cxx17__normal_iteratorIPS0_S2_EERKS0_+0xd2): In function `std::vector<Foo, std::allocator<Foo> >::_M_insert_aux(__gnu_cxx::__normal_iterator<Foo*, std::vector<Foo, std::allocator<Foo> > >, Foo const&)':
  13. : undefined reference to `Foo::~Foo()'
  14. /tmp/ccFq6Xda.o(.gnu.linkonce.t._ZNSt6vectorI3FooSaIS0_EE13_M_insert_auxEN9__gnu_cxx17__normal_iteratorIPS0_S2_EERKS0_+0xef): In function `std::vector<Foo, std::allocator<Foo> >::_M_insert_aux(__gnu_cxx::__normal_iterator<Foo*, std::vector<Foo, std::allocator<Foo> > >, Foo const&)':
  15. : undefined reference to `Foo::~Foo()'
  16. collect2: ld returned 1 exit status
  17. [root@root TEST]#
复制代码

谢谢先。不胜感激!
发表于 2006-3-14 15:12:36 | 显示全部楼层
试试
g++ main.cpp Foo.cpp -o main
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-3-14 19:45:15 | 显示全部楼层

继续请教

谢谢先。上面那个Demo是一个演示的。真正的源码结构跟上面类似,但在编译源码时按照上述的方法,出现这样的错误。

  1. [root@root Project]# g++ main.cpp InfoBrcelyDB.cpp InfoBreclyDBEdit.cpp  -o main

  2. /tmp/ccqMIgyg.o(.gnu.linkonce.r._ZTV15InfoStuEditSelf+0x10): undefined reference to `InfoStuEditSelf::InsertSelf()'
  3. /tmp/ccqMIgyg.o(.gnu.linkonce.r._ZTV15InfoStuEditSelf+0x14): undefined reference to `InfoStuEditSelf::DeleteSelf()'
  4. /tmp/ccqMIgyg.o(.gnu.linkonce.r._ZTV15InfoStuEditSelf+0x18): undefined reference to `InfoStuEditSelf::UpdateScore()'
  5. /tmp/ccqMIgyg.o(.gnu.linkonce.r._ZTV17InfoStuEditScores+0x10): undefined reference to `InfoStuEditScores::InsertScore()'
  6. /tmp/ccqMIgyg.o(.gnu.linkonce.r._ZTV17InfoStuEditScores+0x14): undefined reference to `InfoStuEditScores::DeleteScore()'
  7. /tmp/ccqMIgyg.o(.gnu.linkonce.r._ZTV17InfoStuEditScores+0x18): undefined reference to `InfoStuEditScores::UpdateScore()'
  8. collect2: ld returned 1 exit status
  9. [root@root Project]# ls
复制代码
回复 支持 反对

使用道具 举报

发表于 2006-3-14 22:15:56 | 显示全部楼层
看提示错误, 应该还是有一个源码或都编译后的目标文件没有传给链结程序.
我一般这样用:
A.cpp A.h B.cpp B.h main.cpp
g++ -c A.cpp
g++ -c B.cpp
在main.cpp里include A.h, B.h, 并且会用到A.cpp和B.cpp的函数(否则,就没有必要包含了)
然后
g++ main.cpp A.o B.o -o main
回复 支持 反对

使用道具 举报

发表于 2006-3-14 22:15:58 | 显示全部楼层
看提示错误, 应该还是有一个源码或都编译后的目标文件没有传给链结程序.
我一般这样用:
A.cpp A.h B.cpp B.h main.cpp
g++ -c A.cpp
g++ -c B.cpp
在main.cpp里include A.h, B.h, 并且会用到A.cpp和B.cpp的函数(否则,就没有必要包含了)
然后
g++ main.cpp A.o B.o -o main
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表