LinuxSir.cn,穿越时空的Linuxsir!

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

看一下到底什么地方错了

[复制链接]
发表于 2003-10-1 22:11:17 | 显示全部楼层 |阅读模式

  1. [root@localhost primar]# g++ -Wall Array.cpp
  2. /tmp/ccnkS4SL.o(.text+0x22): In function `main':
  3. : undefined reference to `Array<int>::Array[in-charge](int)'
  4. /tmp/ccnkS4SL.o(.text+0x33): In function `main':
  5. : undefined reference to `Array<double>::Array[in-charge](int)'
  6. /tmp/ccnkS4SL.o(.text+0x44): In function `main':
  7. : undefined reference to `Array<char>::Array[in-charge](int)'
  8. /tmp/ccnkS4SL.o(.gnu.linkonce.d._ZTV5ArrayIcE+0x14): undefined reference to `Array<char>::sort()'
  9. /tmp/ccnkS4SL.o(.gnu.linkonce.d._ZTV5ArrayIcE+0x18): undefined reference to `Array<char>::min() const'
  10. /tmp/ccnkS4SL.o(.gnu.linkonce.d._ZTV5ArrayIcE+0x1c): undefined reference to `Array<char>::max() const'
  11. /tmp/ccnkS4SL.o(.gnu.linkonce.d._ZTV5ArrayIcE+0x20): undefined reference to `Array<char>::find(char const&) const'
  12. /tmp/ccnkS4SL.o(.gnu.linkonce.d._ZTV5ArrayIdE+0x14): undefined reference to `Array<double>::sort()'
  13. /tmp/ccnkS4SL.o(.gnu.linkonce.d._ZTV5ArrayIdE+0x18): undefined reference to `Array<double>::min() const'
  14. /tmp/ccnkS4SL.o(.gnu.linkonce.d._ZTV5ArrayIdE+0x1c): undefined reference to `Array<double>::max() const'
  15. /tmp/ccnkS4SL.o(.gnu.linkonce.d._ZTV5ArrayIdE+0x20): undefined reference to `Array<double>::find(double const&) const'
  16. /tmp/ccnkS4SL.o(.gnu.linkonce.d._ZTV5ArrayIiE+0x14): undefined reference to `Array<int>::sort()'
  17. /tmp/ccnkS4SL.o(.gnu.linkonce.d._ZTV5ArrayIiE+0x18): undefined reference to `Array<int>::min() const'
  18. /tmp/ccnkS4SL.o(.gnu.linkonce.d._ZTV5ArrayIiE+0x1c): undefined reference to `Array<int>::max() const'
  19. /tmp/ccnkS4SL.o(.gnu.linkonce.d._ZTV5ArrayIiE+0x20): undefined reference to `Array<int>::find(int const&) const'
  20. collect2: ld returned 1 exit status
复制代码

第一次遇到这样的错误,晕啊~
发表于 2003-10-1 22:31:06 | 显示全部楼层
光从错误信息还看不出来到底是怎么回事。
 楼主| 发表于 2003-10-1 22:35:39 | 显示全部楼层
Array.cpp
  1. #include <iostream>
  2. #include "Array.h"
  3. //using namespace std;
  4. using std::cout;
  5. using std::endl;

  6. int main()
  7. {
  8.   const int array_size = 4;
  9.   Array<int> ia(array_size);
  10.   Array<double> da(array_size);
  11.   Array<char> ca(array_size);
  12.   int ix;
  13.   for ( ix = 0; ix<array_size; ++ix )
  14. {
  15.   ia[ix] = ix;
  16.   da[ix] = ix*1.75;
  17.   ca[ix] = ix+ 'a';
  18. }
  19.   
  20. for ( ix = 0;ix<array_size; ++ix )
  21. cout<<"["<<ix<<"] iz:"<<ia[ix]
  22.     <<"\tca:"<< ca[ix]
  23.     <<"\tda:"<< da[ix] <<endl;
  24.    return 0;
  25. }

复制代码

Array.h

  1. template < class elemType >
  2. class Array{
  3. public:
  4.    explicit Array( int size = DefaultArraySize );
  5.    Array( elemType *array, int array_size );

  6.    Array( const Array &rhs );

  7.    virtual ~Array(){ delete []ia;}
  8.    bool operator==( const Array& ) const;
  9.    bool operator!=( const Array& ) const;
  10.    Array& operator=( const Array& );
  11.    int size() const { return _size; }
  12.    virtual elemType& operator[](int index){ return ia[index]; }
  13.    virtual void sort();
  14.    virtual elemType min() const;
  15.    virtual elemType max() const;
  16.    virtual int find( const elemType &value ) const;
  17. protected:
  18.    static const int DefaultArraySize = 12;
  19.    int _size;
  20.    elemType *ia;
  21. };

复制代码
发表于 2003-10-1 22:41:42 | 显示全部楼层
只有这两个文件吗?
你写的Array类的成员函数都没有实现。
 楼主| 发表于 2003-10-1 22:54:31 | 显示全部楼层
就这两个,那几个virtual都暂时不需要,结果应该是

  1. [0] ia:0 ca:a da:0
  2. [1] ia:1 ca:a da:1.75
  3. [2] ia:2 ca:a da:3.5
  4. [3] ia:3 ca:a da:5.25
复制代码
 楼主| 发表于 2003-10-1 22:56:05 | 显示全部楼层
这个错误,我是第一次遇到,系统给的,看不出是什么地方错了,晕~
发表于 2003-10-1 23:05:20 | 显示全部楼层
上面的连接问题就是找不到这些成员函数的定义,你要创建对象就必须提供成员函数的定义。
发表于 2003-10-2 00:25:20 | 显示全部楼层
f you use template
then you must define the function realize in .h not .cpp
                                                                                
such as
he.h
ttemplate<class T>
class ccc{
  int get();
  };
                                                                                
  ttemplate<class T>
  ccc<T>:get(){
  return 1000;
  }
                                                                                
                                                                                
  #end .h
~
~
~
发表于 2003-10-2 01:05:15 | 显示全部楼层
对virtual的成员函数应该提供定义,哪怕给个最简单的、没用的也行。
还有,你的explicit Array( int size = DefaultArraySize );,这个可是用到了,如Array<int> ia(array_size);,所以这个也必须提供定义。
 楼主| 发表于 2003-10-2 17:12:46 | 显示全部楼层
谢谢各位斑竹!用g++的时候有什么parameter能跟踪自己写的code吗?
知道他们的变化,今天写了个计算幂的,编译通过了,但是得出的答案都是0,

  1. #include <iostream>
  2. using namespace std;
  3. int val;
  4. int power;
  5. int res;

  6. void pow(int,int)
  7. {
  8. int res=1;
  9.    for ( int cnt = 1;cnt <= power; ++cnt)
  10.     res=val * res;
  11. }

  12. int main()
  13. {
  14.   cin>>val;
  15.    cin>>power;
  16.   pow (val, power);
  17.    cout << res <<endl;
  18. }

复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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