I have this work, and finally finished it with only a header. I have to have .cpp and .h files. However when I try to split my header, I get the following error. Original file runs without any problems as it is, but I really need both .cpp and .h files.
error: it says multiple definition of root and its first declaration is at test.o file, however there are no 'root' string in my test file
1 2 3 4 5
Trie.o:(.bss+0x0): multiple definition of `root'
test.o:(.bss+0x0): first defined here
collect2: error: ld returned 1 exit status
makefile:6: recipe for target 'test' failed
make: *** [test] Error 1
however there are no 'root' string in my test file
Yes there is. Your test file includes that header, so your test file contains int root = 0;
#include is copy-pasting a header into wherever you wrote #include. As if you typed in the whole header file yourself. So everywhere you have #included this header file, you have written this: int root = 0;
So how many cpp files contain int root = 0;? How many times have you tried to create the exact same object, root, in your program? You're only allowed to create the exact same object ONCE in a program.
While I'm here, putting usingnamespace std; anywhere is bad practice, and putting it in a header file will get you beaten up in the car park by anyone who has to work with your code.