0

I am working on a basic BST project for my own reference. I have three files: BST.h, BST.cpp and main.cpp.

Inside BST.h it looks like so:

#pragma once
class BST {
private:
    struct node {
        int key; int data; node* left; node* right };
    node* root;
    node* CreateLeaf(int key, int data);
    void AddLeafPrivate(int key, int data, node* newNode);
    void PrintInOrderPrivate(node* nodePtr);
public:
    BST();
    void AddLeaf(int key, int data);
    void PrintInOrder();
};

And inside BST.cpp I have the formal definitions for all of these functions, etc... an example being:

#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include "BST.h"

using namespace std;

BST::BST() {
    root = NULL;
}

the main.cpp file is currently empty. It only has includes and main() with return 0.

When I try to build this solution, I get LNK2005 errors for each of the functions I have created, something that didn't seem to happen until after I had finished the code for the PrintInOrderPrivate definition. I know that LNK2005 is typically caused by multiple definitions of functions across files, but I don't define anything in BST.h, only prototype. The errors all look something like this:

Severity Code Description Project File Line Suppression State Error LNK2005 "public: __thiscall BST::BST(void)" (??0BST@@QAE@XZ) already defined in BST.obj BasicBinarySearchTreeReference C:\Users\guita\documents\visual studio 2015\Projects\BasicBinarySearchTreeReference\BasicBinarySearchTreeReference\main.obj 1

I also get a LNK1169 error that I can't decipher.

Any insight as to why I am getting these errors is greatly appreciated, I'm really trying to understand BST's better, and this is currently in my way.

Thanks!

templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
  • 1
    Are you including `BST.cpp` in `main.cpp`? Never include `.cpp` (source) files, only header files (`.h`). –  Oct 10 '16 at 00:46
  • I am. If I am to not include it in main.cpp, then I suppose all definitions must be carried out in the same .cpp as the main function, or I'd need to change the BST.cpp file to a header file? – Origin Saint Oct 10 '16 at 00:50
  • 1
    Usually every .cpp file is compiled as individual compilation unit and linked together later (The IDE/Build system usually takes all .cpp files in the project tree). If you include `BST.cpp` in `main.cpp`, then one object file `BST.obj` and one object file `main.obj` are generated, both including the same definitions from `BST.cpp`. Then the linker finds both and gives you the error. Not including source files is the correct thing to do as long as both .cpp are compiled as individual units (which is likely default and convention). –  Oct 10 '16 at 00:54
  • Is it possible to keep the 'main.cpp' file separate and use it solely for interfacing purposes, or does it just make more sense overall to roll the 'main()' function into the BST.cpp? – Origin Saint Oct 10 '16 at 00:56
  • No thats all fine, just include `BST.h` instead of `BST.cpp` in `main.cpp`. –  Oct 10 '16 at 00:57

0 Answers0