here met an error in writing c++ in multiple files.
I am searching for a long time on net. But no use. Please help or try to give some ideas how to achieve this. Thanks in advance!
I designed two class:btNode
and binaryTree
in btNode.h
and binaryTree.h
respectively. Then the member functions are implemented in their own cpp files. and then I defined an object in main.cpp and call the member functions.
below is the result and error when I run my makefile
anc@anc:~/qs/my-Leetcode/code/binaryTreeClass$ make
g++ -c -o main.o main.cpp
g++ -c -o btNode.o btNode.cpp
g++ -c -o binaryTree.o binaryTree.cpp
g++ -o test main.o btNode.o binaryTree.o
main.o: In function `main':
main.cpp:(.text+0x26): undefined reference to `binaryTree<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >::binaryTree()'
main.cpp:(.text+0x69): undefined reference to `binaryTree<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >::insert(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
main.cpp:(.text+0xca): undefined reference to `binaryTree<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >::insert(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
main.cpp:(.text+0x12b): undefined reference to `binaryTree<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >::insert(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
main.cpp:(.text+0x18c): undefined reference to `binaryTree<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >::insert(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
main.cpp:(.text+0x1e7): undefined reference to `binaryTree<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >::insert(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
main.o:main.cpp:(.text+0x23f): more undefined references to `binaryTree<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >::insert(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)' follow
main.o: In function `main':
main.cpp:(.text+0x2c8): undefined reference to `binaryTree<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >::_current_os'
main.o: In function `binaryTree<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >::preorder(std::ostream&)':
main.cpp:(.text._ZN10binaryTreeINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEE8preorderERSo[_ZN10binaryTreeINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEE8preorderERSo]+0x29): undefined reference to `btNode<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >::preorder(btNode<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >*, std::ostream&) const'
main.o: In function `binaryTree<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >::clear()':
main.cpp:(.text._ZN10binaryTreeINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEE5clearEv[_ZN10binaryTreeINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEE5clearEv]+0x2a): undefined reference to `binaryTree<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >::clear(btNode<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >*)'
collect2: error: ld returned 1 exit status
makefile:5: recipe for target 'test' failed
make: *** [test] Error 1
and here is my makefile:
objects = main.o btNode.o binaryTree.o
cc = g++
test : $(objects)
$(cc) -o test $(objects)
main.o : binaryTree.cpp binaryTree.h btNode.cpp btNode.h
binaryTree.o : binaryTree.h btNode.cpp btNode.h
btNode.o : btNode.h binaryTree.h
.PHONY:clean
clean:
-rm test $(objects)
@echo "rm test files..."
here is one of the two header file
#ifndef _BTNODE_H
#define _BTNODE_H
#include<iostream>
using namespace std;
template <typename elemType>
class binaryTree;
template<typename valType>
class btNode{
friend class binaryTree<valType>;
public:
btNode(const valType &val) : _val(val) {
_cnt = 1;
_lchild = _rchild = 0;
}
const valType& value() const {return _val;}
int occurs() const {return _cnt;}
void insert_val(const valType&);
void remove_val(const valType&, btNode*&);
void inorder(btNode* , ostream&) const;
void preorder(btNode* , ostream&) const;
void postorder(btNode* , ostream&) const;
static void lchild_leaf(btNode*, btNode*);
private:
valType _val;
int _cnt;
btNode *_lchild;
btNode *_rchild;
void display_val(btNode*, ostream&) const;
};
#endif
main.cpp
#include<iostream>
#include<string>
#include"binaryTree.h"
using namespace std;
int main() {
binaryTree<string> bt;
bt.insert("Piglet");
bt.insert("Eeyore");
bt.insert("Roo");
bt.insert("Tigger");
bt.insert("Chris");
bt.insert("Pooh");
bt.insert("Kanga");
cout << "Preorder traversal:\n";
bt.preorder();
return 0;
}
The question is when I write all the code in ONE file(just sum of all the code,no modify), there is no problem and obtain the desired results.
But I want to use the style of multi file programming, because it's good. pls help me find the problem, thanks very much.
Aucun commentaire:
Enregistrer un commentaire