1

I have created an empty project and added two ( .cpp) items inside the project. Keep in mind that I am a beginner in C++ Visual Studio, so I have not used any code to somehow connect the two files.

Problem: The debugger was fine for debugging my first file, but when I open my second file and start running by clicking "Local Windows Debugger" (clicked when I was inside the second file), it will still keep on running the first file whether there was a bug or not.

When I looked at the debug window after I hit "Local Windows Debugger", I saw the file path pointing to the first file.

I have tried: Closing the first file completely, closing visual studio and opening my second file from my folder path, turning off the break-point in the first file and turning on in the second file.

I would like to know: How can I just run the second file? Do I have to use the command prompt to keep my two items in the empty project separate? I am using Windows 10 by the way.

I searched for my problem, but I had a hard time looking for a guide that gave a solution

Dracep
  • 360
  • 2
  • 11
  • 1
    What's your folder/file hierarchy and what are the contents of both scripts? [You should check here](https://stackoverflow.com/help/how-to-ask) if you need a guide on good Q/A etiquette. – James Whyte May 16 '18 at 03:43
  • 1
    The start of the program is always the `main` function. Each project has only one `main` in all its files. – DeiDei May 16 '18 at 03:45
  • @DeiDei jeez, I had both of my files have a main function. Should I use void as an alternative for my second file? – Dracep May 16 '18 at 03:48
  • 1
    @JamesWhyte My apologies, I did not realize that the content of my code as in having two main functions would affect which file would debug first. Do you know a good read on different file starters and how I can get a specific file in a project to debug first? – Dracep May 16 '18 at 03:51

1 Answers1

1

Issue:

Dracep cannot debug one of two files they have. This is due to them having a main function definition in both files, causing the editor to favour one over the other.

Solution:

By having a third, dedicated entry point in your application (I.E. having a single point of entry you then include the other files), you can decide which file you are going to debug at any given time.

For example, having a file called main.cpp which then includes the other two files by using #include "filename.h".

From there you can include the file and make you code checks by calling the functions in that file rather than having a main and stepping down through it, causing long term issues of scalability.

Please see this question on separating your logic from your definitions, as the answer marked correct is the standard for most C++ projects you will find.

That way, you could do something like the following:

#include "File1.h"
#include "File2.h"

int main(int argc, char** argv)
{
    File1Class file1Class;
    File2Class file2Class;
    //Do whatever tests you like with either.
}
James Whyte
  • 678
  • 3
  • 13
  • 1
    By following this method and separating your logic from your definitions, you can debug to your heart's content without significant issue. It's a bit of a pain to learn, but Headers and CPP files are exactly what you should strive for; they make life EZ-PZ. ;D – James Whyte May 16 '18 at 04:06
  • 1
    Thanks! Up to now I have focused on only the basic syntax of C++, but it's showing that I need to look at the fundamentals as well. – Dracep May 16 '18 at 04:12