dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: [RESOLVED] LNK2005 ERROR on Visual C++

  1. #1
    Join Date
    Jan 2018
    Posts
    7

    [RESOLVED] LNK2005 ERROR on Visual C++

    Hi Guys,
    I hope everyone is well. I'm having trouble with this error Name:  LNK2005.jpg
Views: 98
Size:  14.1 KB. Not sure what to do, can anyone assist. Below is my code; header, cpp and main.

    Code:
    //Header file
    
    #ifndef BST_H
    #define BST_H
    #include "stdafx.h"
    # include <iostream>
    # include <stdlib.h>
    using namespace std;
    
    class BST
    {
    private:
    
    	struct node
    	{
    		int key;
    		node* left;
    		node* right;
    	};
    
    	node* root;
    
    	void AddLeafPrivate(int key, node* Ptr);
    	void PrintInOrderPrivate(node* Ptr);
    
    public:
    
    	BST(); //Constructor to be called anytime we create a binaryTree (BT) object
    	node* CreateLeaf(int key); // create leaf node to return a node pointer that references the leaf node
    	void AddLeaf(int key);
    	void PrintInOrder();
    };
    #endif

    Code:
    //BST.cpp file
    
    #include "stdafx.h"
    #include <iostream>
    #include <cstdlib>
    
    #include "BST.h"
    
    using namespace std;
    
    
    
    
        BST::BST()
    	{
    		root = NULL;
    	}
    	BST::node* BST::CreateLeaf (int key)
    	{
    		node* n = new node;
    		n->key = key;
    		n->left = NULL;
    		n->right = NULL;
    
    		return n;
    	}
    
    
    	void BST::AddLeaf(int key)
    	{
    		AddLeafPrivate(key, root);
    	}
    	
    	void BST::AddLeafPrivate(int key, node* Ptr)
    	{
    		if(root ==NULL)
    		{
    			root = CreateLeaf(key);
    		}
    		else if (key < Ptr->key)
    		{
    			if(Ptr->left !=NULL)
    			{
    				AddLeafPrivate(key, Ptr->left);
    
    			}
    			else
    			{
    				Ptr->left = CreateLeaf(key);
    			}
    		}
    			else if (key > Ptr->key)
    		{
    			if(Ptr->right !=NULL)
    			{
    				AddLeafPrivate(key, Ptr->right);
    
    			}
    			else
    			{
    				Ptr->right = CreateLeaf(key);
    			}
    		}
    			else
    			{
    				cout << "The key " << key << " has already been added to the tree\n";
    			}
    	}
    
    	void BST::PrintInOrder()
    	{
    		PrintInOrderPrivate(root);
    	}
    
    	void BST::PrintInOrderPrivate(node* Ptr)
    	{
    		if (root != NULL)
    		{
    			if(Ptr->left !=NULL)
    			{
    				PrintInOrderPrivate(Ptr->left);
    			}
    			cout << Ptr->key << " ";
    			if(Ptr->right !=NULL)
    			{
    				PrintInOrderPrivate(Ptr->right);
    			}
    		}
    		else
    		{
    			cout << "The tree is empty\n";
    		}
    	}
    Code:
    // BinarySearchTree.cpp : Defines the entry point for the console application.
    #include "stdafx.h"
    #include <iostream>
    #include <cstdlib>
    
    #include "BST.cpp"
    
    using namespace std;
    
    
    int main ()
    {
    	int TreeKeys[16] = {51, 53, 54, 51, 32, 1, 89, 81, 74, 5, 23, 55, 23, 33, 11, 33};
    	BST myTree;
    
    	cout << "Printing the tree in order\n Before adding numbers\n";
    
    	myTree.PrintInOrder();
    
    	for (int i = 0; i < 16; i++)
    	{
    		myTree.AddLeaf(TreeKeys[i]);
    	}
    	cout << "Printing the tree in order\n After adding numbers\n";
    
    	myTree.PrintInOrder();
    
    	system ("pause");
    	return 0;
    }

  2. #2
    Join Date
    Jan 2018
    Posts
    7

    Re: LNK2005 ERROR on Visual C++

    The image is kind of tiny, below are the errors.

    1>BST.obj : error LNK2005: "public: __thiscall BST::BST(void)" (??0BST@@QAE@XZ) already defined in BinarySearchTree.obj
    1>BST.obj : error LNK2005: "public: struct BST::node * __thiscall BST::CreateLeaf(int)" (?CreateLeaf@BST@@QAEPAUnode@1@H@Z) already defined in BinarySearchTree.obj
    1>BST.obj : error LNK2005: "public: void __thiscall BST::AddLeaf(int)" (?AddLeaf@BST@@QAEXH@Z) already defined in BinarySearchTree.obj
    1>BST.obj : error LNK2005: "private: void __thiscall BST::AddLeafPrivate(int,struct BST::node *)" (?AddLeafPrivate@BST@@AAEXHPAUnode@1@@Z) already defined in BinarySearchTree.obj
    1>BST.obj : error LNK2005: "public: void __thiscall BST::PrintInOrder(void)" (?PrintInOrder@BST@@QAEXXZ) already defined in BinarySearchTree.obj
    1>BST.obj : error LNK2005: "private: void __thiscall BST::PrintInOrderPrivate(struct BST::node *)" (?PrintInOrderPrivate@BST@@AAEXPAUnode@1@@Z) already defined in BinarySearchTree.obj
    1>c:\users\matayo family\documents\visual studio 2010\Projects\BinarySearchTree\Debug\BinarySearchTree.exe : fatal error LNK1169: one or more multiply defined symbols found
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

  3. #3
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,199

    Re: LNK2005 ERROR on Visual C++

    #include "BST.cpp" // <<-- include BST.h, not BST.cpp
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: http://codexpert.ro/blog/author/ovidiu-cucu/

  4. #4
    Join Date
    Jan 2018
    Posts
    7

    Re: LNK2005 ERROR on Visual C++

    Quote Originally Posted by ovidiucucu View Post
    #include "BST.cpp" // <<-- include BST.h, not BST.cpp
    Genius!!!

    That fixed it! Can't believe how crazy it drove me.

    Thank you very much for your time and response.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)