C/C++ How to handle the Specification and Implementation files to be used in C++?

  • Thread starter Thread starter yungman
  • Start date Start date
  • Tags Tags
    files
Click For Summary
The discussion revolves around the challenges of organizing and integrating C++ class files, specifically the header (.h) and implementation (.cpp) files, while using Visual Studio. The user expresses frustration over needing to use absolute paths for includes, which complicates file management. Suggestions include keeping all project files in a single folder to simplify includes, as well as utilizing Visual Studio's project properties to set additional include directories. The conversation highlights the importance of modular code organization, with a focus on separating class declarations and definitions for better management. There are also discussions about the potential for confusion when compiling separate files and the benefits of learning to manage project structures effectively. The user ultimately finds a method that works for them, though they acknowledge the learning curve and inconsistencies in Visual Studio. They also reflect on the broader concepts of object-oriented programming and the importance of understanding class structures as they progress in their studies.
  • #31
yungman said:
Don't ask me why Even what Mark44 suggested in POST#9 only partially work. I had to modified like this to make it work.
You must have made a mistake. I would go back and try again, try to see what strange thing you did to mess it up.
 
Technology news on Phys.org
  • #32
Jarvis323 said:
You must have made a mistake. I would go back and try again, try to see what strange thing you did to mess it up.
Thanks for the reply.

I tried it many times. BUT, I have to tell you VS behaves better after I re-installed it 3 weeks ago, but it's still not the most consistent program.

I made sure I copied the EXACT Rectangle.h or RectConstr.h that I put the "C:\\...", it just didn't work. It was consistent also with both Rectangle.h and RectConstr.h ( they are different, one without and one with constructor).

I could have swear it worked the first time after Mark suggested, but I cannot repeat it.

And I do Rebuild Project, Clear Project to make sure it is working. I even exit the program. Only thing I did not do is restarting the laptop.

My way is NOT the best way as I have to copy and paste, I don't like that at all. I like Mark's way if I can make it work. Until I learn a better way, I just get on with life. Hopefully Mark can comment on this. I REALLY NOT the expert in VS to put it politely!
 
  • #33
Also, I experience quite a few times when I was working on binary files. The book use different exercise problems, one to create the file, one to read the file, AND then one to modify the file. So you would expect to create the file with data in the first one, I can read the file with the second one. BUT I kept failing. I can read back the file, BUT I cannot modify the file.

I think I even posted a program that I had the first part that I put it in comment AFTER I use it to write the file. The file had to be written in the SAME program, or else it won't work. All I did was copy the whole code of the first program that write the file from the other problem, no changes at all. It just didn't work if I just copy and paste the binary file over.

In my book, VS have issues. I might be new in programming, I use a lot, I mean a lot of programs like circuit simulations, microwave simulation, OrCad, PADS and other pcb layout programs. I never encounter so much inconsistent issues like VS. Even free programs like LTSpice and some pcb program free. Don't tell me VS is more sophisticate, Those microwave simulation and pcb layout programs are big, can do 3D simulations, 18+ layer pcb and simulate the signals. Some cost over $10K.

Don't ask me why, I did not even ask the question here. I think I asked too many questions here already.
 
Last edited:
  • #34
yungman said:
Also, I experience quite a few times when I was working on binary files. The book use different exercise problems, one to create the file, one to read the file, AND then one to modify the file. So you would expect to create the file with data in the first one, I can read the file with the second one. BUT I kept failing. I can read back the file, BUT I cannot modify the file.

I think I even posted a program that I had the first part that I put it in comment AFTER I use it to write the file. The file had to be written in the SAME program, or else it won't work. All I did was copy the whole code of the first program that write the file from the other problem, no changes at all. It just didn't work if I just copy and paste the binary file over.

In my book, VS have issues. I might be new in programming, I use a lot, I mean a lot of programs like circuit simulations, microwave simulation, OrCad, PADS and other pcb layout programs. I never encounter so much inconsistent issues like VS. Even free programs like LTSpice and some pcb program free. Don't tell me VS is more sophisticate, Those microwave simulation and pcb layout programs are big, can do 3D simulations, 18+ layer pcb and simulate the signals. Some cost over $10K.

Don't ask me why, I did not even ask the question here. I think I asked too many questions here already.
I feel I have to give some honest feedback. I mean no offense.

1) VS with vc, or any c++ compiler, is WAY more sophisticated than (likely) any simulation code in existence. Compiler engineers make upward of 400k a year because of the advanced knowledge and skill it requires. Those qualifications are very rare and hard to acquire and maintain. As an example, gcc (which is a free open source compiler) is more than 14 million lines of non-trivial code and would probably cost at least 200 million to 500 million dollars worth of labor (probably a lot more) to develop. And that's just the compiler, which is only one tool integrated into an IDE. It also doesn't include the work done by the standards committee, research done at universities that has been incorporated, and so forth. And the estimated cost to write those 14+ million lines also represent only the current gcc code, without consideration to code that has been replaced or updated over the years.

2) The largest and most sophisticated C++ programs tend to free and/or open source. Many costly programs tend to be smaller, niche programs with small user bases.

3) Every inconsistency or issue you have run into so far has been your own inconsistency or issue, NOT a problem with the software you're using. We warned you that Visual Studio is sort of complicated and that C++ is an extremely complicated language. It takes patients and humility (and working through lots of mistakes) to get comfortable with it.
 
Last edited:
  • #35
Jarvis323 said:
I feel I have to give some honest feedback. I mean no offense.

1) VS with vc, or any c++ compiler, is WAY more sophisticated than (likely) any simulation code in existence. Compiler engineers make upward of 400k a year because of the advanced knowledge and skill it requires. Those qualifications are very rare and hard to acquire and maintain. As an example, gcc (which is a free open source compiler) is more than 14 million lines of non-trivial code and would probably cost at least 200 million to 500 million dollars worth of labor (probably a lot more) to develop. And that's just the compiler, which is only one tool integrated into an IDE. It also doesn't include the work done by the standards committee, research done at universities that has been incorporated, and so forth. And the estimated cost to write those 14+ million lines also represent only the current g++ code, without consideration to code that has been replaced or updated over the years.

2) The largest and most sophisticated C++ programs tend to free and/or open source. Many costly programs tend to be smaller, niche programs with small user bases.

3) Every inconsistency or issue you have run into so far has been your own inconsistency or issue, NOT a problem with the software you're using. We warned you that Visual Studio is sort of complicated and that C++ is an extremely complicated language. It takes patients and humility (and working through lots of mistakes) to get comfortable with it.
You are most likely right. I don't know enough to say. I can only tell you my experience...which is not much. I will keep an open mind and observe. Thanks for your comment.

I am taking it a little easy the last two days, I know not to push too hard. So far, I am still ok with the Class in Chapter 13. I'll see. I hope things can get easier as I learn more.

I want to emphasize, I really appreciate you and the few others that take the time to help me.

Thanks
 
  • #36
I copied what you posted in post #6, with one small change (eliminated the long path on the header file), and the program worked as expected. No muss, no fuss, no gyrations.

Of course the portion with main() has to have an #include for the header file -- the header file contains declarations for the class and its methods that you are using. And of course, the implementation file needs an #include for the header file, since the implementation is providing definitions for the class methods.
In both the user of the class (with main()) and the implementation, this is all you need as declarations for the class and its methods (functions):
C++:
#include "Rectangle.h"
You really don't want that long full path you had in post #6.
 
  • Like
Likes yungman
  • #37
yungman said:
In my book, VS have issues. I might be new in programming, I use a lot, I mean a lot of programs like circuit simulations, microwave simulation, OrCad, PADS and other pcb layout programs.
These are all applications, and are very different from a program or suite of programs that are used to create an application.
yungman said:
I never encounter so much inconsistent issues like VS.
I'm inclined to agree with @Jarvis323 here.
Jarvis323 said:
Every inconsistency or issue you have run into so far has been your own inconsistency or issue, NOT a problem with the software you're using.
 
  • Like
Likes Vanadium 50
  • #38
Mark44 said:
I copied what you posted in post #6, with one small change (eliminated the long path on the header file), and the program worked as expected. No muss, no fuss, no gyrations.

Of course the portion with main() has to have an #include for the header file -- the header file contains declarations for the class and its methods that you are using. And of course, the implementation file needs an #include for the header file, since the implementation is providing definitions for the class methods.
In both the user of the class (with main()) and the implementation, this is all you need as declarations for the class and its methods (functions):
C++:
#include "Rectangle.h"
You really don't want that long full path you had in post #6.
Thanks for your reply

I tried. Now I do something funky, just follow what I did.
1) I created a new project and called Temp. I right click the Header file --> ADD --> NEW Item --> Header file which I named Temp.h. I then copy the header file from post 6 in.
2) I then right click the Source File --> ADD --> NEW Item --> C++. I called Temp.cpp. I copied the .cpp file from post 6 in. I then closed the Temp project.

3) I went into the folder of Temp project and copy both Temp.h and Temp.cpp into a new folder somewhere else. AND I DELETED the whole Temp project folder.

4) I then create a new empty project T1. I added the both Temp.h and Temp.cpp. like you described by ADD-->Exist file and search and add both of them. Making sure Temp.cpp has #include "Temp.h".

5) Then I right click Source File--> ADD --> New Item --> C++, I copied the Source file from post 6 in. I change to #include "Temp.h".

I ran and it failed.

Here is the snap shot of the Source.cpp that shows red wiggled line under #include "Temp.h".
Compile error Listing 7.2.jpg


Then I ran Ctrl-F5. This is the error message.
Compile error Listing 7.3.jpg


I circled on the right side that I have Temp.h in it.I double checked all 3 files and they look right.

I did these steps because as I described in post #33. There is always some strange things going on.
To verify this.

1)I DELETED T1.

2) Create a new project T2.

3) In T2, I ADD the Temp.h, Temp.cpp and Source.pp STRAIGHT from the folder I saved them in( the same as before). I make sure Source.cpp has #include "Temp.h".

4) Ran it, it works like a Champ.
You see what I did different between the two? If I copied everything into one project straight from the folder, it works. BUT, if I create a DIFFERENT project(T1) and ADD the Temp.h and Temp.cpp, then I copy the same Source.cpp in, it doesn't recognize the Temp.h.

I know this issue for a few days already, I just never bother to ask as I have asked too many questions here already. Again, thanks for your patience in keep helping me.

Thanks
 
Last edited:
  • #39
I did more-or-less what you described in post #38, and again, it worked just as it should.
The only thing I can see is that possibly the program structure as shown in VS doesn't match the actual file structure on the hard drive. You copied several files, but if you copied them to the wrong directory, VS is not going to be able to find them.

I created a new project named Temp. I then deleted the file that was in there, a nearly empty file named Temp.cpp. I copied Rectangle.h, Rectangle.cpp, and RectImpl.cpp as shown in the following screen shot of Solution Explorer in VS.
SolnExpl.png


Here is a screen shot of the file structure, as shown in Windows Explorer.
FileExpl.png

The file structure should look like this:
Temp directory
... Temp.sln
... Debug directory (after a successful build, assuming your are in Debug mode). It should contain ....Temp.exe and a couple of other files used by the debugger.
... Temp directory (an inner directory with the same name as the outermost directory
... Two source code files - Rectangle.cpp and RectImpl.cpp, and the header file Rectangle.h
... Two or three files that VS uses - Temp.vcxproj, Temp.vcxproj.filters

I suspect that all your copying and deleting of files screwed things up so that VS can't find the files it needs to build the program.

Open Windows Explorer and see what your file structure looks like. If it doesn't look like what I've shown here, that's what's causing the error you see.
 
  • #40
Hi Mark
I created a Test folder that I stored the Temp.h, Temp.cpp and source.cpp from a WORKING program.

The difference is in the Temp project, I right click the Header files and ADD existing files and go to Test folder to get the Temp.h. Then I right click the Source file and ADD existing files and go to Test folder and get Temp.cpp.

THEN I open a NEW project T1, take in the source.cpp from the Test folder . I COPY to code of the source.cpp. I then go back to Temp project, ADD NEW file called source.cpp as the main file. Then I PASTE the code I copied from T1 in. It failed. It's the same code. Here are the 3 screen to show you the code of all 3 files.
TempSource.cpp.jpg

Notice the red wiggle line?
This is the Temp.cpp the implementation file.
TempTemp.cpp.jpg
This is the Temp.h the Specification file
TempTemp.h.jpg
Now, this project is called Project2. I use ADD--> Existing file and go to Test folder to get ALL 3 files. It work like a champ. Mind you, in both Project2 and Temp above, the codes are EXACTLY THE SAME. Here are the screen shots of the 3 Project2 files. You can be the judge to compare the codes to verify:

Proj2 source.cpp.jpg


Proj2 Temp.cpp.jpg


Proj2 Temp.h.jpg
 
  • #41
Let me sum it up my experience. In order for the program to work, ALL 3 files has to be created the same way. Like in project2, I use ADD existing file and go to Test folder to get all 3 files.

In Temp project that failed. I use ADD existing file and go to Test folder to get only Temp.h and Temp.cpp. THEN in order to make the source.cpp coming from a different place. I created a new project T1 just to get the source.cpp into the project, THEN just use Ctrl-C to copy the code. Then I go back to Temp, ADD--> New-->.cpp to create a blank source.cpp. Then I Ctrl-V and copy the code into it. It failed.Like I said, this is not new to me, a few days ago I did 3 exercise, first exercise is to create 5 structures with data in each of them. The second exercise is to read the 5 back. They are work. The 3rd exercise is to read the 5, choose to change one of them. IT FAILED. Even if I copy the binary file from the first exercise to the folder of the 3rd exercise, it FAILED.

I finally had to just copy and paste the code from the first exercise into the 3rd exercise, run the code to create the 5 structures store in the folder of the 3rd exercise. Then delete the code. Now I can read and change anyone of 5 as will.

This is NOT new to me. that's why I know how to make it fail. All I have to do is to have the source.cpp created from a different source from the Specification and Implementation files, it will fail.

Different source is just copy into another project first, then copy over. Just that simple.

thanks
 
  • #42
If you #include "x.h" in a source file, then either x.h needs to be in the same directory as that source file, or you need to tell the compiler where to look for header files by adding to the list of include search paths.

It makes sense that you would have the error if the source file is in one place and the headers are in another, and visual studio is not generating the compiler arguments to add the location of the headers to the search path. You can manually add those directories to the search path as I pointed out in previous posts. You can look at the actual compiler command and see if the include search path was added.

https://stackoverflow.com/questions...-the-build-command-line-used-by-visual-studio

You would think "add existing file" might automatically trigger VS adding a new include directory to the search path if necessary.

Maybe some of the answers here will help as well. I noticed the last one suggests individual cpp files might have their own properties (e.g. additional include directories) separate from the project properties. Or maybe you added the file only for release or debug mode instead of for all configurations.

https://stackoverflow.com/questions/3790632/visual-studio-does-not-honor-include-directories

Or Mark mentioned something about setting up a "common" directory. I don't know the way people are supposed to handle this in VS exactly.

Also, don't mistake the folders in VS for real folders. The files have an actual location on your computer, which is what actually matters.
 
Last edited:
  • #43
Hi Jarvis, Thanks for the reply. It's late to go over your long post tonight, I'll read into it tomorrow.

Regarding to where to put the .h and .cpp files, there is nothing wrong where I put it. You can see from the 6 screen captures in post 40, they are all there and show exactly where the files are. All the codes are in the exact right place as shown and all the codes are identical between the working Project2 and the non working Temp.

It is very repeatable also, Like earlier on, I replied in post #38, I created the file that failed and post in #38. I then deleted all of them. When Mark responded, I had to create both files, one working and one failed. I can just create them one time through. It's that predictable.

Of cause, It can still be me making the same mistake over and over. That's why I showed in post #40 exactly the two program, every piece of code and show where those files are. They are identical.

Thanks
 
  • #44
yungman said:
Hi Jarvis, Thanks for the reply. It's late to go over your long post tonight, I'll read into it tomorrow.

Regarding to where to put the .h and .cpp files, there is nothing wrong where I put it. You can see from the 6 screen captures in post 40, they are all there and show exactly where the files are. All the codes are in the exact right place as shown and all the codes are identical between the working Project2 and the non working Temp.

It is very repeatable also, Like earlier on, I replied in post #38, I created the file that failed and post in #38. I then deleted all of them. When Mark responded, I had to create both files, one working and one failed. I can just create them one time through. It's that predictable.

Thanks
Yeah, but the key is that VS is not showing you where the files actually are. Those aren't real folders. Add existing file doesn't move any files.
 
  • Informative
Likes pbuk
  • #45
yungman said:
Regarding to where to put the .h and .cpp files, there is nothing wrong where I put it. You can see from the 6 screen captures in post 40, they are all there and show exactly where the files are. All the codes are in the exact right place as shown and all the codes are identical between the working Project2 and the non working Temp.
None of the screen shots you posted shows the actual file structure. That's why one of the screen shots I posted was from Windows Explorer, not VS.
yungman said:
Of cause, It can still be me making the same mistake over and over. That's why I showed in post #40 exactly the two program, every piece of code and show where those files are. They are identical.
No, your screen shots did not show where those files are. In post #39, at the bottom, I wrote this, which you apparently didn't read. Many of the problems that you have encountered were due to not reading what others wrote.

Mark44 said:
Open Windows Explorer and see what your file structure looks like. If it doesn't look like what I've shown here, that's what's causing the error you see.
Please reread what I wrote in post #39. Below, @Jarvis323 is saying the same thing that I said in post #39.

Jarvis323 said:
Yeah, but the key is that VS is not showing you where the files actually are. Those aren't real folders. Add existing file doesn't move any files.
 
Last edited:
  • Like
Likes Vanadium 50
  • #46
I think that something @Jarvis323 is key to your (@yungman's) difficulties.
Jarvis323 said:
You must have made a mistake. I would go back and try again, try to see what strange thing you did to mess it up.
If you (@yungman) find that a poster's advice isn't working for you, go back and re-read the post with the advice. This has happened enough times that it bears repeating. Although it's possible that the advice is wrong (we're all human), I would venture to say that there's probably about 5% chance that the advice is in error, and 95% chance that you didn't read all of the advice or follow it correctly.
 
  • Like
Likes Vanadium 50
  • #47
Hi Mark

I don't mean to be offensive. I know what you mean about looking at the file explorer. I am going to post the screen shot of both the Project2(working) and Temp(not working) later.

I don't think you read my description carefully enough. My whole point is if the source.cpp are from different places where the Temp.h and Temp.cpp come from, it won't work even if you read them, they are IDENTICAL.

I describe in detail TWICE already, first in post 38, then again in post 40 and 41, in very detail how I created the two project. I show you in post 40 that on the right side, you see all 3 files there. AND if you click anyone of them, you can SEE the codes of EACH of them. They are there. I screen shot all 3 codes in both Project2 and Temp, you can see they show EXACTLY the same. One work and one doesn't Please look at them again. How can this go so wrong if I went through all these to show they are there?Regarding to what you ask. I looked and NONE of them have the Temp.h and Temp.cpp. The working one Project2 don't even have source.cpp. They are so far off I did not even mention it. Here are the screen shot, both are wrong according to you, I have no explanation why Project2 work.

Here is the Temp that DOESN'T work:
Temp file explorer.jpg

Here is the Project2 that WORKS.
Proj2 file explorer.jpg
You see the Project2 doesn't even have a single file? BUT IT RUNS.

I am open to be wrong (as usual), I put in a lot of time on this, why don't they contain those files? Why can't I do ADD--> Existing file and go to the folder to get the file. Project2 was done by that, it works, it ran but still don't show any of the .cpp and .h in the file explorer?

Thanks
 
Last edited:
  • #48
I went back and look at the old folders, those I created as described in post #30 all have source.cpp, Rectangle.h and Rectangle.cpp in the file explorer. I redo a new project, BUT instead of ADD--> Existing and load the file. I COPIED those codes, using ADD-->New and paste them in. It works and in file explorer, it shows all 3 files like you want to see.

WHY? Why it won't show if I do ADD-->Existing and navigate to another folder to get the file? Why it has to be copy and paste? the method I used in post 30 all needs to be copy into the newly created blank files. Both ways, you can see the files are there, you can click and see their codes. Why?
 
  • #49
yungman said:
I went back and look at the old folders, those I created as described in post #30 all have source.cpp, Rectangle.h and Rectangle.cpp in the file explorer. I redo a new project, BUT instead of ADD--> Existing and load the file. I COPIED that codes, using ADD-->New and paste them in. It works and in file explorer, it shows all 3 files like you want to see.

WHY? Why it won't show if I do ADD-->Existing and navigate to another folder to get the file? Why it has to be copy and paste? the method I used in post 30 all needs to be copy into the newly created blank files.
We answered this question in detail multiple times already.
 
  • #50
Jarvis323 said:
We answered this question in detail multiple times already.
Why it run nicely with Project2 if it is all wrong?
 
  • #51
yungman said:
Why it run nicely with Project2 if it is all wrong?
Did you find the "additional include directories" property for your project, and for your source files individually?
 
  • #52
Before I start digging into the file stuffs. I ran more experiments based on looking at the file explorer and to find out why the files don't show up when Project2 works.

This is what I did. With Project 2 that I use ADD-->Existing file to go to Test folder to get those files. I went to Test folder and temporary deleted the Temp.h. Project2 FAILED to compile. This means the Project2 NEVER pull in the files, just by reference(if that's the right word). That might be the reason why it doesn't show up in file explorer.

If I do copy then ADD-->New, You will see all 3 files in the file explorer like what Mark show.

So the result is, either I do all ADD-->Existing for all 3 files, OR ADD-->New and copy the codes for all 3 files. They BOTH WORK. BUT if I mix some ADD-->Existing, some ADD-->New and copy, it FAIL.

Why?

This go beyond digging through the files and all. VS obviously gives the provision of ADD-->New and ADD-->Existing. How come there is limitation? Checking into the files ONLY shows the result/symptom, WHY ADD NEW and ADD Existing cannot be combined? This is more troubling to me.

Now, I am going to look into what Jarvis said. I still don't believe they are the same.
 
Last edited:
  • #53
yungman said:
Why?
How many times will you keep asking why and ignore the answer?

Because the source file needs to be in the same directory as the include file, or the "additional include directories" of the source file needs to have the directory where the header files added.

This is the last time I'll respond to any of your posts. Good luck.
 
  • Like
Likes Vanadium 50
  • #54
yungman said:
WHY? Why it won't show if I do ADD-->Existing and navigate to another folder to get the file?
Add --> Existing item... doesn't copy files to the directories for the project you're working on. The file is added to the project and will be shown in the VS Project Explorer, but the file will still be in whatever directory it already was in.

What you should be doing is Add --> New item... This creates a blank file with the name you choose, and puts it in the right directory. Then you can copy the text from the old file, and paste it into the file you've created.
 
  • #55
Mark44 said:
Add --> Existing item... doesn't copy files to the directories for the project you're working on. The file is added to the project and will be shown in the VS Project Explorer, but the file will still be in whatever directory it already was in.

What you should be doing is Add --> New item... This creates a blank file with the name you choose, and puts it in the right directory. Then you can copy the text from the old file, and paste it into the file you've created.

Thanks for the answer

I know that. At this point this is NOT my question anymore as I posted in post #52.

My question is if it is so important to have the file physically inside the project( to show in file explorer). Why then Project2 run just fine using ALL Add-->Existing?

Why then the exact same code FAILED if I mix Add-->Existing with Add-->New? It has to be either ALL Add-->Existing OR Add-->New?I have been searching in VS, I found Exporting Template so others can use it. Is that the right way in the future to link Class Object to be used in the program? I've been trying to use Add--> Existing thinking that it's better to link external files from another folder.
Thanks
 
Last edited:
  • #56
Jarvis323 said:
How many times will you keep asking why and ignore the answer?

Because the source file needs to be in the same directory as the include file, or the "additional include directories" of the source file needs to have the directory where the header files added.

This is the last time I'll respond to any of your posts. Good luck.
I am not ignoring your answers, I actually looked back to your posts. I understand you and Mark said I need to have all 3 files inside the project using file explorer as Mark said in post #39. If I do Add-->New and copy all the files in. This, I can see in file explorer all 3 files are there.

My questions are why Project2 works without any of the files in the project folder then? I use Add-->Existing that doesn't actually pull the files in.

AND the most important question is why then if I MIX Add-->Existing and Add-->New, it fail no matter what?

I hope you can reconsider, you have been very helpful and I cannot thank you enough.

Thanks
 
Last edited:
  • #57
Hi Jarvis

You posted a video and I notice you deleted the post. I was watching the video, there's nothing similar to the video with my VS. I even tried starting with Console App or Window Desktop Wizard, they are nowhere the same as in the video. I cannot follow it. VS 2019 make it very automatic. I know how to add in the .h and .cpp file no problem at all. That's not in question.

Thanks anyway.
 
  • #58
yungman said:
Hi Jarvis

You posted a video and I notice you deleted the post. I was watching the video, there's nothing similar to the video with my VS. I even tried starting with Console App or Window Desktop Wizard, they are nowhere the same as in the video. I cannot follow it. VS 2019 make it very automatic. I know how to add in the .h and .cpp file no problem at all. That's not in question.

Thanks anyway.
I guess you misread all of my responses.

When you "add existing" to all three files, all three files are in the same directory (not in the project directory, but the cpp is in the directory that the h is in). That's why it works.

I posted the video because it shows how to put header files in a separate place as a cpp file. VS 2019 might have a different GUI, but it works the same way. You just need to figure out where the "additional include directories" setting is. I bet it would take 5 minutes to find it if you actually looked for it.

Until you figure that out, you're just wasting your time. It's like you keep walking into a closed door because you refuse to listen when someone tells you how to open it.

I deleted the video because, although it shows how to organize h and cpp files separately, the author added the path to his "src" folder in the "additional include directories", which I think doesn't make sense and could lead to confusion about how things actually work.

VS apparently allows you to set different "additional include directories" for each cpp file. This makes sense, because you might have different header files in different places with the same name, and need to control which one is included by which cpp file. VS designers had to make a choice what the default behavior is in different cases. I guess the way you are using it, the default is to NOT add the path to the header file in the "additional include directories" property of the cpp file. So you have to do that manually, or you have to figure out how to change the VS default, or do things a different way so that the default is what you wanted.
 
Last edited:
  • Like
Likes yungman
  • #59
Jarvis323 said:
I guess you misread all of my responses.

When you "add existing" to all three files, all three files are in the same directory (not in the project directory, but the cpp is in the directory that the h is in). That's why it works.

I posted the video because it shows how to put header files in a separate place as a cpp file. VS 2019 might have a different GUI, but it works the same way. You just need to figure out where the "additional include directories" setting is. I bet it would take 5 minutes to find it if you actually looked for it.

Until you figure that out, you're just wasting your time. It's like you keep walking into a closed door because you refuse to listen when someone tells you how to open it.

I deleted the video because, although it shows how to organize h and cpp files separately, the author added the path to his "src" folder in the "additional include directories", which I think doesn't make sense and could lead to confusion about how things actually work.

VS apparently allows you to set different "additional include directories" for each cpp file. This makes sense, because you might have different header files in different places with the same name, and need to control which one is included by which cpp file. VS designers had to make a choice what the default behavior is in different cases. I guess the way you are using it, the default is to NOT add the path to the header file in the "additional include directories" property of the cpp file. So you have to do that manually, or you have to figure out how to change the VS default, or do things a different way so that the default is what you wanted.
After a search, I found that the "additional include directories" option is in the C++ General tab for VS 2019.
https://docs.microsoft.com/en-us/cpp/linux/prop-pages/general-linux?view=vs-2019
 
  • Like
Likes yungman
  • #60
yungman said:
My questions are why Project2 works without any of the files in the project folder then? I use Add-->Existing that doesn't actually pull the files in.
For a project named Temp, VS keeps track of where all the files are in a file named Temp.vcxproj. This is a text file, so you can read it. It's not a good idea for you to edit it, if you don't know what you're doing. You can find the .vcxproj file for your project in the 2nd-level directory for your project, which will normally have the same name as the top-level directory.
yungman said:
AND the most important question is why then if I MIX Add-->Existing and Add-->New, it fail no matter what?
I don't know. This is not enough information to go on. I've described a way that works, so why are you exploring weird ways that you know don't work.
yungman said:
My question is if it is so important to have the file physically inside the project( to show in file explorer).
It's not important, other than to keep different projects separate from each other. The files that make up a multi-file project can be pretty much anywhere, as long as there is information about their locations in <project_name>.vcxproj, but that's not a good way to organize things.
All of your programs have been built from single projects. More complex programs can be made up of multiple projects. At the top level in Solution Explorer is the Solution, which is named Temp in your case. This solution could be made up of two or more projects, each with its own Header Files section and Source Files section, as well as its own References, External Dependencies, and Resource Files sections.
yungman said:
Why then Project2 run just fine using ALL Add-->Existing?
The project won't build if you don't save all the files -- so that the .vcxproj file gets updated. If it gets updated, then things should work just fine, but if you build before this file is updated, then you're get compile errors. That's all I can think of.
The only time I use Add -> Existing item... is when I have already copied the file into the appropriate directory, as viewed in Windows Explorer. Then I use Add --> Existing item...
 
  • Like
Likes yungman

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 0 ·
Replies
0
Views
2K
  • · Replies 33 ·
2
Replies
33
Views
3K
  • · Replies 6 ·
Replies
6
Views
12K
Replies
6
Views
2K
Replies
4
Views
5K
  • · Replies 8 ·
Replies
8
Views
4K