The System Cannot Find the File Specified Visual Studio 2019

First time posting so apologies for any mistakes.
I am following a tutorial as part of a school assignment (before anyone jumps on me for that, we are required to follow this tutorial and then turn it in without modifying anything) on Binary Search Trees. It is a playlist made by Paul Programming (https://youtu.be/sf_9w653xdE) and while being older it seems to work just fine. Unfortunately, I am using Visual Studio as a course requirement and the tutorial does not. He uses a bash terminal in his, and so I am following along by creating the three files (BST.cpp, BST.h, and main.cpp) that he does in the Header and Source file locations. It seems that the IDE does not allow me to do so successfully as immediately I get this error whenever I click the Local Window Debugger button:
"Unable to start program
'G:\File Path\BinarySearchTreex64\Debug\BinarySearchTree.exe' (File Path is just a stand in for my file path on my USB, there is just several files I did not want to include as they are not really relevant.)

The system cannot find the file specified."

This is accompanied by the Output from the Build:
1
2
3
4
5
6
1
1>BST.obj : error LNK2005: "public: __cdecl BST::BST(void)" (??0BST@@QEAA@XZ) already defined in main.obj
1>BST.obj : error LNK2005: "public: void __cdecl BST::AddLeaf(int)" (?AddLeaf@BST@@QEAAXH@Z) already defined in main.obj
1>BST.obj : error LNK2005: "private: void __cdecl BST::AddLeafPrivate(int,struct BST::node *)" (?AddLeafPrivate@BST@@AEAAXHPEAUnode@1@@Z) already defined in main.obj
1>BST.obj : error LNK2005: "public: struct BST::node * __cdecl BST::CreateLeaf(int)" (?CreateLeaf@BST@@QEAAPEAUnode@1@H@Z) already defined in main.obj
1>BST.obj : error LNK2005: "public: void __cdecl BST::PrintInOrder(void)" (?PrintInOrder@BST@@QEAAXXZ) already defined in main.obj
1>BST.obj : error LNK2005: "private: void __cdecl BST::PrintInOrderPrivate(struct BST::node *)" (?PrintInOrderPrivate@BST@@AEAAXPEAUnode@1@@Z) already defined in main.obj


I have searched through several posts about people having similar issues but could not find anything helpful and nothing regarding this specific tutorial. Any help would be greatly appreciated.
Try reading the official microsoft message for linker error LNK2005 and see if it applies - it probably will.

https://docs.microsoft.com/en-us/cpp/error-messages/tool-errors/linker-tools-error-lnk2005?view=msvc-160
Hello N11 Ordo Skirata,

You did not include your 3 files, so I can only guess at what you have done.

My first guess is:
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
// + Others

#include "Bst.h"

#include "BST.cpp"  // <--- If this is here it should not be.

int main()
{

}


The linker error leads me to believe that you are trying to link the same code twice.

In Vs when you first start yo create a solution. the solution can contain 1 or more projects, usually 1 for what you are doing, and a project can contain 1 or more files, such as (BST.cpp, BST.h, and main.cpp). The project considers all these file as separate. At compile time the ".h" file will be added to the ".cpp" files that have an include statement for this header. Unless the header file has an include guard the will only compile the file once. When the files are linked anything that is missing or duplicated will cause an error.

My first guess is that you are trying to include a >cpp" file in a ".cpp" file and this is not the correct way to do this.

Andy
I did try looking over the errors but I do not understand how to implement those fixes, as well as changing something would effect my grade. Would the problems I am having have to do with the older versions of C++ or a newer version of VS not implementing things the same way? Or the fact that the tutorial is using a bash terminal to rather than VS?

Also, am I correct in assuming that the BST.h file should be in the header section and the BST.cpp and main.cpp be in the source section?
Andy,

currently BST.cpp has
1
2
3
4
5
6
#include <iostream>
#include <cstdlib>

#include "BST.h"

using namespace std;


and main.cpp has
1
2
3
4
5
6
#include <iostream>
#include <cstdlib>

#include "BST.cpp"

using namespace std;



This is the way the tutorial I am supposed to follow has done it, so if it is wrong can you please explain further?
Hello N11 Ordo Skirata,

Saying that the VS2019 was set up correctly for this program and working correctly I would question what you are watching. The code for main may have worked in the tutorial or in the tutorial "BST.cpp" may not have been compiled and that is why it is included in "main".

Give this a try: In "main" line 4 put a comment on that line and then see if the program will compile and link. I have a feeling that it will.

To use what you have the "Bst.cpp" file should not be a part to the solution/project. Then it would just be a file that is included in the "main" file when compiled Which BTW is is not the proper way to do this.

Andy

Edit: typos
Last edited on
so if it is wrong can you please explain further?

It is not necessarily wrong to include cpp files, but it is unconventional -- if a beginner does this it is typically an error.

Your project's arrangement assumes that only main.cpp is compiled. This is an example of a unified build:
https://en.wikipedia.org/wiki/Unity_build


Last edited on
main.cpp:
1
2
3
4
5
6
7
#include <iostream>
#include <cstdlib>

#include "BST.cpp"

using namespace std;


The "correct" thing would be to change the 4th line to #include "BST.h" and I think you'd have it fixed. The IDE will manage linking the .cpp files as long as you add them to the project properly.

Since this is an assignment and including that cpp file is a requirement, try right clicking on that line. You should see a menu pop up with the option to add the file to the include path. Try that out.

If that wasn't an option, or it didn't fix the problem, then you might have to go into the project properties and remove existing linkages to the BST.cpp file so that it's not linked multiple times.
Edit: I've edited this several times and did a full rewrite just a few minutes ago, it looks like Handy Andy already explained how to remove the file from the project without deleting said file. That might do what I'm recommending in this last paragraph... I don't use Visual studio, so I deffer to him on this.
Last edited on
Hello N11 Ordo Skirata,


Would the problems I am having have to do with the older versions of C++ or a newer version of VS not implementing things the same way? Or the fact that the tutorial is using a bash terminal to rather than VS?


No, No and No.

I have the feeling it is in the way that you created your solution and project for this program.

If I am understanding what you said in the "Solution Explorer" it is saying that under the "Header Files" sextion you have "Bst.h" and under the "Source Files section" you have "Bst.cpp" and "main.cpp".

To follow the tutorial and comply with what you need highlight the "Bst.cpp" file and press the "Delete" key or right click and choose "Remove". When the window pops up choose "Remove" to remove the file from the project, but keep the file. The "Delete" button will remove the file from the "Source Files" and permenantly delete the file, not usually what you want.


Also, am I correct in assuming that the BST.h file should be in the header section and the BST.cpp and main.cpp be in the source section?


Yes and yes.

Andy

Edit:
Last edited on
Thank you to all of you that have responded so far. I will try some of these fixes soon as other homework has come up. I will also try my professor who was unreachable at the time, thus the forum question. I'll add an update when I have the opportunity to try these fixes.

Update:
The solution of just replacing BST.cpp with BST.h worked. Everything is right in the world once again. Thank you to everyone who responded.
Last edited on
Topic archived. No new replies allowed.