undefined reference to 有关问题
undefined reference to 问题
一个简单的建树,打印树的程序。。上眼。。
报错:
/tmp/ccP9q7K0.o: In function `main':
/home/jzh/ProgrammerExcerse/Main.cpp:8: undefined reference to `Act<char>::Init(node<char>*)'
/home/jzh/ProgrammerExcerse/Main.cpp:9: undefined reference to `Act<char>::Print(node<char>*)'
collect2: ld 返回 1
不明白阿。。。。。求解
------解决方案--------------------
貌似除了EDG前端以外的编译器(包括常见的VC++、g++等)都不支持模版分离编译,C++11里export也被deprecated了,所以模版定义就不要放.cpp了,全放.h里解决。
------解决方案--------------------
mark
一个简单的建树,打印树的程序。。上眼。。
- C/C++ code
//Tree.h 1 #include<iostream> 2 using namespace std; 10 template < typename T> 11 struct node{ 12 //friend class Act; 13 T data; 14 node< T > *lch; 15 node< T > *rch; 16 node(); 17 //node( const T &x ); 18 ~node(); 19 }; 20 21 template < typename T > 22 class Act{ 23 public: 24 void Init( node< T >* tr ); 25 void Print( node< T >* tr ); 26 }; //Tree.cpp 1 #include<iostream> 2 #include"Tree.h" 3 using namespace std; 4 template < typename T > 5 void Act< T >::Init( node< T >* tr ) 6 { 7 tr = new node< char >; 8 cout << "输入data : " << flush; 9 cin >> tr -> data; 10 if ( tr -> data != "*" ) 11 { 12 Init( tr -> lch ); 13 Init( tr -> rch ); 14 } 15 } 16 template < typename T > 17 void Act< T >::Print(node< T >* tr ) 18 { 19 if( tr -> data != "*" ) 20 { 21 cout << tr -> data << endl; 22 Print(tr -> lch ); 23 Print(tr -> rch ); 24 } 25 } //Main.cpp 1 #include<iostream> 2 #include"Tree.h" 3 using namespace std; 4 int main() 5 { 6 node< char >* root; 7 Act< char > act; 8 act.Init( root ); 9 act.Print( root ); 10 11 return 0; 12 }
报错:
/tmp/ccP9q7K0.o: In function `main':
/home/jzh/ProgrammerExcerse/Main.cpp:8: undefined reference to `Act<char>::Init(node<char>*)'
/home/jzh/ProgrammerExcerse/Main.cpp:9: undefined reference to `Act<char>::Print(node<char>*)'
collect2: ld 返回 1
不明白阿。。。。。求解
------解决方案--------------------
貌似除了EDG前端以外的编译器(包括常见的VC++、g++等)都不支持模版分离编译,C++11里export也被deprecated了,所以模版定义就不要放.cpp了,全放.h里解决。
------解决方案--------------------
mark