Linker Error in Dev-C++

Hi, I just joined the community!
I have been implementing a Binary Search Tree Class. There are two classes, one is BSTNode which only has data,*left & right. Methods are functions to manipulate the tree are in BST.

While i was trying to compile using Dev-C++ 4,9,9,2 I receive the error of:
[Linker error] undefined reference to `BSTNode::BSTNode(int const&, BSTNode*, BSTNode*)'
It does not indicate the line, and my all methods are defined inline, I do not get why the error contains "BSTNode::"..

Here is the relevent part of the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
using namespace std;

class BSTNode
{
    public:
    BSTNode* left;
    BSTNode* right;
    int item;
    BSTNode (const int &item, BSTNode *lptr=NULL, BSTNode *rptr=NULL ); // Constructor for BSTNode class
};
class BST
{
      public:     
      BST(const int item, BSTNode *root=NULL);
      int InsertValue(BSTNode *root, int newitem)            // Inserting an integer to an existing tree..      
      {
           if ( root == NULL )
           root=new BSTNode(newitem);
                               
           else  
                  {
            if ( newitem < root->item )               // If new value is smaller
            InsertValue(root->left, newitem );      // Add to the left.
            else                                     // If new value is greater or equal 
            InsertValue(root->right, newitem );     // Add to the right.
                   }   
      }
sounds like it couldn't find the definition of the constructor in your program. Maybe you left it out, that happens to me sometimes as well.

root=new BSTNode(newitem);
I don't see a constructor for BSTNode with a single parameter inside your class as well. This would make it undefined also.
Last edited on
You are right. I'm quite a starter, i wonder if i can overload the constructor?
like;

1
2
BSTNode (const int &item, BSTNode *lptr=NULL, BSTNode *rptr=NULL );
BSTNode (const int &item)
yeah you can overload constructors in c++.
And welcome to the community. I'm pretty new here myself ^_^
Last edited on
After overloading still same error :D
But anyway that constructor seems to be the root of the error, thanks for your help.
You can't overload the constructor like that since the first one has defaults for the last two parameters which would result in an ambiguous call if you attempted to write the line of code Dacster13 gave.

Where is the implementation of the constructor defined on line 10 in the original code?
There were some global scale methods and there were pointers defined as BSTNode in main function but i did not paste here since it will be too long and messy.
Do you guess it is something with implementation of constructor?
Yes, it was indeed about the implementation of the constructor..
Thanks for notice jsmith.

Previously it was:

1
2
BSTNode::BSTNode(const int &item, BSTNode *lptr=NULL, BSTNode *rptr=NULL){
}


Now;

1
2
BSTNode::BSTNode(const int &item, BSTNode *lptr, BSTNode *rptr){
}


But i do not quite get why NULL s caused error. Now it compiles.
Topic archived. No new replies allowed.