0

I'm trying to write a program that creates a Binary Tree data structure but I'm getting these weird compiler errors:

main.o:main.cpp:(.text+0x15): undefined reference to `BSTree::BSTree()'
main.o:main.cpp:(.text+0x15): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `BSTree::BSTree()'
collect2: error: ld returned 1 exit status

I don't really have any experience with this sort of compiler error and can't really figure out what could be wrong. As far as I can tell I've written similar code for other projects which compile fine.

This is my code:

main.cpp

#include <iostream>
#include <cstdlib>
#include "BSTree.h"
using namespace std;
int main()
{
    BSTree treeValues;
    return 0;
}

BSTree.h

#include <cstdlib>
#include "BTNode.h"
using namespace std;
class BSTree
{
    public:
    BSTree();
    void add(int);
    private:
    BTNode* m_root;
    int m_size;
};

BSTree.cpp

BSTree::BSTree()
{
    m_root = NULL;
    m_size = 0;
}
void BSTree::add(int it)
{
    m_root = new BTNode(it);
}

BTNode.h

#include <cstdlib>
using namespace std;
class BTNode
{
    public:
    BTNode(int);
    private:
    int m_data;
    BTNode* m_left;
    BTNode* m_right;
    BTNode* m_parent;
};

BTNode.cpp

BTNode::BTNode(int data)
{
    m_data = data;
    m_left = NULL;
    m_right = NULL;
    m_parent = NULL;
}

EDIT: fixed error message and formatting on .cpp files

p.chives
  • 125
  • 5
  • 1
    If the file you claim produces this problem is indeed `main.cpp` (as shown in the question), why does your error message claim some file, `TreeHashTableDemo.cpp`, which is *none* of the files shown here, as the culprit using an undefined symbol ? – WhozCraig Nov 05 '18 at 07:01
  • What you post here is working. If you actually have `cpp` files for the tree then you need to link them to the main program. – perreal Nov 05 '18 at 07:02
  • I didn't indicate the cpp files correctly so I've added them to my original post. But I'm still getting the error. – p.chives Nov 05 '18 at 07:09
  • If that's your entire code, you should be getting some compilation errors before that linker error. – molbdnilo Nov 05 '18 at 07:10
  • This is my full code. I just get this weird compiler error but nothing explicit regarding line numbers etc. – p.chives Nov 05 '18 at 07:12
  • compile the tree code and link it with the main file. – perreal Nov 05 '18 at 07:13
  • because it's **linking error** and not compiler error. Your code has been compiled properly. You need to show your compile command – phuclv Nov 05 '18 at 07:14
  • What do you mean? I'm compiling them together? – p.chives Nov 05 '18 at 07:14
  • 1
    Well, what compiler are you using -- and -- are you using an IDE or command line? (presume you are using an IDE, because if you had been using the command line -- it would be apparent what is wrong). Are you using VS Code, gcc, clang -- what? – David C. Rankin Nov 05 '18 at 07:22

1 Answers1

2
  1. You shouldn't use using namespace std; in header files
  2. You should add #include "BTNode.h" in BTNode.cpp
  3. You should add #include "BSTree.h" in BSTree.cpp
hellow
  • 12,430
  • 7
  • 56
  • 79
Yunbin Liu
  • 1,484
  • 2
  • 11
  • 20
  • this fixed it, thanks – p.chives Nov 05 '18 at 07:23
  • Can you please extend your answer and link to some stackoverflow pages ([this](https://stackoverflow.com/questions/4872373/why-is-including-using-namespace-into-a-header-file-a-bad-idea-in-c) or [this](https://stackoverflow.com/questions/5849457/using-namespace-in-c-headers)) and extend 2 and 3 why one should/have to do that. – hellow Nov 05 '18 at 07:42