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
AI Thread 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.
yungman
Messages
5,741
Reaction score
294
I am learning how to create Classes. I already created the Specification file(Rectangle.h) and Implementation file(member function Rectangle.cpp) separately. In the book the CD that came with it supposed to have ways to do it, BUT of cause I bought used and don't have the CD!

I already know how to #include Rectangle.h into the Implementation file. It's simple, just #include ""C:\\Users\\alanr\\Desktop\\C++ exercise\\Gaddis\\inout files\\Rectangle.h" for my directory in the laptop.....BUT it's so clumsy!

In a complete program that use this Class, I have to pull in the Specification file( header file) and Implementation file to use in the program, There must be an easier and cleaner way to do it like we just #include<iostream>, none of the C:\\... stuffs. You guys have a standard way of doing this easier?

Do you create a folder to store all the header files and implementation file and use a special way to call them without having to writhe all the "C:\\..." stuffs?

EDIT: I forgot to say, I know you can physically copy the .h and .cpp file into the program using it. To me, that's even worst than writing "C:\\..." because you actually go and copy and paste every time you write a program that use the Class Object. There got the be a better way.

Thanks
 
Last edited:
Technology news on Phys.org
There are a number of different ways to organise files, some of which depend on settings in Visual Studio. For now you should just include all the files for a project in a single folder and then you can just #include "Rectangle.h".
 
pbuk said:
There are a number of different ways to organise files, some of which depend on settings in Visual Studio. For now you should just include all the files for a project in a single folder and then you can just #include "Rectangle.h".
Thanks

But I still have to have both files in the same place. So I have no choice but to copy those from the specification program and the implementation program into the new project that uses it. TWhat if I need to change the .h file, I have to do it over again?

Or just don't worry about it, just do it this way and wait for later when I learn other ways?

Thanks
 
This has nothing to do with C++. You can have everything in one big .cpp file if you wish. Many find this to be a less than ideal organization, but it's perfectly legal.

It has more to do with Visual Studio.
 
What operating system are you working on and what are you using to compile? Are you using MS Visual Studio? Whatever tools you are using, there are always ways to put the header files in one directory and keep the implementation code someplace else. That is the only way that a large group of programmers can formally agree on the interfaces before the implementation code is completed.
 
This is question about VS, I am running on Windows 10. I have no issue understanding what this whole thing trying to do and the code. I just don't know how to put them together.

I am trying to integrate the Specification, implementation file to work together with the program that uses it. I have no idea how to integrate them together. The book just skip this all together and act as if it's supposed to just work! I typed in the Rectangle.h file and Rectangle.cpp file. There is no instruction in running the compiler on these two files. I have no idea what to do. I already put the rectangle.h into the calling project in the same folder as the source.cpp of the project. But there is no instruction what to do with the implementation file Rectangle.cpp. I copy the Rectangle.h, Rectangle.cpp and the source.cpp here. Can anyone help me? I don't have the CD from the book.

This is the Rectangle.h that define the Rectangle Class Object.
C++:
//Specification file for Rectangle
#ifndef Rectangle_H
#define Rectangle_H
//Rectangle class declaration
class Rectangle
{
private:
    double width;
    double length;
public:
    void setW(double);
    void setL(double);
    double getW() const;
    double getL() const;
    double getA() const;
};
#endif //#pragma once
This is the Rectangle.cpp That has all the member functions.
C++:
//Rectangle implementation file
#include "C:\\Users\\alanr\\Desktop\\C++ exercise\\Gaddis\\inout files\\Rectangle.h"
#include <iostream>
#include <cstdlib>
using namespace std;

void Rectangle::setW(double w)
{
    if (w >= 0)    width = w;
    else { cout << "Invalid width.\n\n"; exit(EXIT_FAILURE); }
}
void Rectangle::setL(double len)
{
    if (len >= 0) length = len;
    else { cout << "Invalid length.\n\n"; exit(EXIT_FAILURE);
    }
}
double Rectangle::getW() const
{    return width;}
double Rectangle::getL() const
{    return length;}
double Rectangle::getA() const
{    return width * length;}
This is the source.cpp of the program that call the Rectangle class.
C++:
// 13.4 using Rectangle Object .h and .cpp
#include <iostream>
#include "Rectangle.h" //Needed for Rectangle Class
using namespace std;int main()
{
    Rectangle box; //define an instance of Rectangle Class
    double rectW;//Local variable
    double rectL;

    cout << " this program will calculate the area of rectangle.\n\n";
    cout << " Enter width = "; cin >> rectW;
    cout << " Enter Length = "; cin >> rectL;
    cout << endl;
    //Store width and length into Box Object
    box.setW(rectW); box.setL(rectL);

    //Display the rectangle data.
    cout << " Width of rectangle is = " << box.getW() << endl;//function return width
    cout << " Length of rectangle is = " << box.getL() << endl;//function return length
    cout << " Area of rectangle is = " << box.getA() << "\n\n";//return area.
    return 0;
}

These are very simple codes, I already ran it with them all in the Same .cpp ( not separate into .h and all, everything contain in one program). Now is just how to separate them and run to get the same result.

Thanks
 
Jarvis323 said:
I guess the place you add your include directories should be here: Tools / Options / Projects and Solutions / VC++ Directories / Include files
https://stackoverflow.com/questions/2676417/how-do-include-paths-work-in-visual-studio
https://docs.microsoft.com/en-us/cpp/build/reference/vcpp-directories-property-page?view=vs-2019
Thanks for the reply
I followed your direction, it's not the same. I read the first link, it was about VS in 2008 that has the same path as yours, mine is VS2019, this is where it's different:
Compile error Listing 7.2.jpg


There is no VC++Directories.

I am still searching on the web also. I don't know how relevant is this:
http://www.math.uaa.alaska.edu/~afkjm/csce211/handouts/SeparateCompilation.pdf

Thanks
 
Jarvis323 said:
I guess the place you add your include directories should be here: Tools / Options / Projects and Solutions / VC++ Directories / Include files
No, that's where the VS include files go. You shouldn't clutter up the VS system files with programs you're working on..

Organizing a project with multiple source files and one or more header files is pretty straightforward.
SolnExplorer.png

When you start a new project, the Header Files folder will be empty, and the Source Files Folder will be empty. If you want to add a header file (.h), right-cick Header Files, and Add --> New item will cause a dialog box to open. In it, select Header file, and enter a name for it, then click Add.

To add a source code file, right-click Source Files, and Add --> New item will cause the same dialog box to open. Select Header file, enter a name for the file, then click Add. There's another option to add a Class, but I don't use that option.

Once you have added the source code file(s) and header file(s), you can type your programs in, or paste the text in.

You showed Rectangle.h, Rectangle.cpp, and source.cpp for the project you're working on. Rectangle.h has the class declaration in it, Rectangle.cpp has the definitions of the member functions, and source.cpp has main().
Rectangle.h would go in the Header Files folder as shown in the screen shot above, and Rectangle.cpp and source.cpp would both go in the Source File folder. As long as there is only one main() function, you can build the project just as you have been doing.

yungman said:
I don't think it's at all relevant to what you're trying to do.
 
  • Like
Likes yungman and FactChecker
  • #10
Mark44 said:
No, that's where the VS include files go. You shouldn't clutter up the VS system files with programs you're working on..

Organizing a project with multiple source files and one or more header files is pretty straightforward.
View attachment 270303
When you start a new project, the Header Files folder will be empty, and the Source Files Folder will be empty. If you want to add a header file (.h), right-cick Header Files, and Add --> New item will cause a dialog box to open. In it, select Header file, and enter a name for it, then click Add.

To add a source code file, right-click Source Files, and Add --> New item will cause the same dialog box to open. Select Header file, enter a name for the file, then click Add. There's another option to add a Class, but I don't use that option.

Once you have added the source code file(s) and header file(s), you can type your programs in, or paste the text in.

You showed Rectangle.h, Rectangle.cpp, and source.cpp for the project you're working on. Rectangle.h has the class declaration in it, Rectangle.cpp has the definitions of the member functions, and source.cpp has main().
Rectangle.h would go in the Header Files folder as shown in the screen shot above, and Rectangle.cpp and source.cpp would both go in the Source File folder. As long as there is only one main() function, you can build the project just as you have been doing.

I don't think it's at all relevant to what you're trying to do.
Thanks a million! If I can, thanks TEN Millions!

You make my day! It's just THAT easy! I was so worry! Now I can go watch some tv!

Thanks
 
  • #11
Mark44 said:
No, that's where the VS include files go. You shouldn't clutter up the VS system files with programs you're working on..
I don't use VS. But usually you have some header files that you use across different programs, or some third party libraries you are using. You have to tell the compiler where they are. In g++ you use -I. There must be some way to do this with VS?
 
  • #12
Jarvis323 said:
I don't use VS. But usually you have some header files that you use across different programs, or some third party libraries you are using. You have to tell the compiler where they are. In g++ you use -I. There must be some way to do this with VS?
I saw that g++ in this:
http://www.math.uaa.alaska.edu/~afkjm/csce211/handouts/SeparateCompilation.pdf
I got in the command file and ran it, it didn't work. It doesn't recognize g++.

You never use VS? I don't know any other ones, the debugger works really good. I had to re-install VS, it seems to work better now. One thing, the projects VS creates are huge...I mean huge! Don't know why it's so big, takes a long time to do backup.
 
Last edited:
  • #13
I am starting to study new material, in the book it said you can compile the implementation file first. The main program can take in the Rectangle.h and the object file of implementation file. How do you compile the implementation file Rectangle.cpp in post #6?

I tried, it flag error because it's missing int main(). All I have to do is add in

int main()
{
return 0;
}
into the Rectengle.cpp, it will compile, but that's not going to work. How do you compile just the Rectangle.cpp?Also, I am reading the inline member functions. If I can put some of the inline function into the Specification file(Rectangle.h), why can't I put all the member functions into the Specification file and save the trouble of having a separate Implementation file?

thanks
 
  • #14
yungman said:
I am starting to study new material, in the book it said you can compile the implementation file first.
...
How do you compile just the Rectangle.cpp?
I am not sure if you are reading this right, in any case as you are using Visual Studio Code then it has its own ways of working for things like this. I suggest you ignore any parts of the book that address the mechanics of compilation and build tools like 'make' and move on.

yungman said:
Also, I am reading the inline member functions. If I can put some of the inline function into the Specification file(Rectangle.h), why can't I put all the member functions into the Specification file and save the trouble of having a separate Implementation file?
You can, but as things get more complicated it becomes easier to manage if you split them so it is a good idea to learn how to do this now.

I have never seen the term 'Specification file', we usually call this a 'header file'.
 
  • Like
Likes yungman
  • #15
pbuk said:
I am not sure if you are reading this right, in any case as you are using Visual Studio Code then it has its own ways of working for things like this. I suggest you ignore any parts of the book that address the mechanics of compilation and build tools like 'make' and move on.You can, but as things get more complicated it becomes easier to manage if you split them so it is a good idea to learn how to do this now.

I have never seen the term 'Specification file', we usually call this a 'header file'.
Must be just my book that call Specification file.
Compile error Listing 7.2.jpg

It also use header file.
 
  • #16
yungman said:
I saw that g++ in this:
http://www.math.uaa.alaska.edu/~afkjm/csce211/handouts/SeparateCompilation.pdf
I got in the command file and ran it, it didn't work. It doesn't recognize g++.

You never use VS? I don't know any other ones, the debugger works really good. I had to re-install VS, it seems to work better now. One thing, the projects VS creates are huge...I mean huge! Don't know why it's so big, takes a long time to do backup.
VS is a nice IDE, but I mainly use a linux operating system and I don't usually use an IDE at all, except Qt creator when I am making a Qt based GUI application.
 
Last edited:
  • #17
Jarvis323 said:
I don't use VS. But usually you have some header files that you use across different programs, or some third party libraries you are using. You have to tell the compiler where they are. In g++ you use -I. There must be some way to do this with VS?
I've written several hundred C++ programs, most of them quite small. None of them share header files, as they are mostly intended for use in the classes I teach, so are examples that focus on a small number of C++ aspects.

If one does write programs in VS that need to share header files, the header files can be placed in a common directory. There is a project property in VS that one can set so that VS knows where to look for header files that aren't in the "usual" place. The same -I switch that you mentioned can also be used in VS if you want to compile the code from the command line.
However, as I said before, IMO it's not a good idea to clutter up the compiler's header file directory with additional header files.
 
  • #18
Mark44 said:
IMO it's not a good idea to clutter up the compiler's header file directory with additional header files.
I used to work on some large projects where I often needed to modify the VS files to change where the header files for different versions were stored. I agree that it is a very bad idea to put your application header files in the directories of the standard libraries.
 
  • #19
It works if you just specify like #include "C:\\...\\Rectangle.h" in both the Rectangle.cpp and source.cpp, I even deleted the Rectangle.cpp in the main program and ADD-->Existing and do "C:\\...\\Rectangle.cpp", then you can put both anywhere you like. It work like a champ. I just changed the source.cpp this way.
C++:
// 13.4 using Rectangle Object .h and .cpp
#include <iostream>
#include "C:\\Users\\alanr\\Desktop\\C++ exercise\\Gaddis\\inout files\\Rectangle.h" //Needed for Rectangle Class
using namespace std;int main()
{
    Rectangle box; //define an instance of Rectangle Class
    double rectW;//Local variable
    double rectL;

    cout << " this program will calculate the area of rectangle.\n\n";
    cout << " Enter width = "; cin >> rectW;
    cout << " Enter Length = "; cin >> rectL;
    cout << endl;
    //Store width and length into Box Object
    box.setW(rectW); box.setL(rectL);

    //Display the rectangle data.
    cout << " Width of rectangle is = " << box.getW() << endl;//function return width
    cout << " Length of rectangle is = " << box.getL() << endl;//function return length
    cout << " Area of rectangle is = " << box.getA() << "\n\n";//return area.
    return 0;
}
 
  • #20
That certainly works. Eventually, you will want to start putting less detailed and specific paths in your include statements. You don't want your code to depend on the exact location of everything. The compiler can search in a list of relative-path directories. That allows you to move your code without needing to change all your include statements.

I would recommend that you have a directory for headers at a high level in that exercise directory and put the relative path in the include like:
#include "..\\..\\includes\\Rectangle.h";
 
  • #21
This is not a good idea, why do you insist on doing things your own way? Doing it your way makes it impossible for anyone else to help you, do it the way everybody else does.

Just in case you are in any doubt: never put absolute paths in source code files.
 
  • #22
pbuk said:
This is not a good idea, why do you insist on doing things your own way? Doing it your way makes it impossible for anyone else to help you, do it the way everybody else does.

Just in case you are in any doubt: never put absolute paths in source code files.
I just keep experimenting until I learn a better way. One gain knowledge by keep experimenting.
 
  • #23
I am 40 pages into Chapter 13, sounds like all the Classes of ADT are very much like function that is EXTERNAL to the main program, that the program call them when there's a need.

The normal functions are still inside the program, just take it outside main() so it does not have to be repeated used over and over at different places, and also make it simpler for people to understand. The Classes take it to another level that you don't even put the code in the main program and just do it by linking. All is just to make the code more modular and break up the big program so different people can work on it at the same time. All they need is to specify the interface and what parameters are being passed.

Ha ha, I found myself talk a little Russian now, all the Object, instance, header, scope resolution operator, dot operator...I am so bad in remembering names, I used to be able to remember people's phone number and forgot their names! It was so hard for me at the beginning here, here you guys trying to explain things to me, I just could not come right out and say you are not helping because your explanations are usually worst than my questions! Reading online were even worst. It takes time to get into this, now that I am on chapter 13, all of a sudden, it's like the fog starts to lift a little. Knock on wood, 40 pages into Chapter 13, I really don't have serious question, only question so far is how to put all the files together and now I got it. Hopefully it remains the same for the rest of the chapter. I am studying Constructors, next is Destructors, then array of Classes. That's the end of the chapter.
 
  • #24
I run into VS problem. You can see below
Compile error Listing 7.2.jpg


On the left top, you see a red wiggle line under #include. It said cannot open RectConstr.h.
With exception of adding the constructor, both the RectConstr.h and RectConstr.cpp are the EXACT same as Rectangle.h and Rectangle.cpp used before.

In the RectConstr.h
C++:
//Specification file for Rectangle with Constructor
#ifndef Rectangle_H
#define Rectangle_H
//Rectangle class declaration
class Rectangle
{
private:
    double width;
    double length;
public:
    Rectangle();//Constructor
    void setW(double);
    void setL(double);
    double getW() const;
    double getL() const;
    double getA() const;
};
#endif //

and RectConstr.cpp
C++:
//Rectangle implementation file with Constructor
#include "C:\Users\alanr\Desktop\C++ exercise\Gaddis\inout files\Rectangle.h"
#include <iostream>
#include <cstdlib>
using namespace std;

Rectangle::Rectangle()
    {width = 0.0; length = 0.0;}
void Rectangle::setW(double w)
{
    if (w >= 0)    width = w;
    else { cout << "Invalid width.\n\n"; exit(EXIT_FAILURE); }
}
void Rectangle::setL(double len)
{
    if (len >= 0) length = len;
    else { cout << "Invalid length.\n\n"; exit(EXIT_FAILURE);}
}
double Rectangle::getW() const
    {return width;}
double Rectangle::getL() const
    {return length;}
double Rectangle::getA() const

You can see I only added line11 Rectangle() in the RectConstr.h and line 7 to 8 in RectConstr.cpp.
The files are all there, I can click the files on the left of the image above and actually show the codes of both the .h and .cpp codes. They are in the program.

Only major change is the name of the files, I use RectConstr instead of Rectangle to distinguish them that they have Constructor in them.

What is the problem here?

thanksEDIT: I have been reading more, the Gaddis book starts to refer to the stuffs in the CD that came with the book. Of cause I don't have the CD. I ended up have to buy another same book that comes with the CD, the same edition. It said it comes with the CD, I hope it's true. Luckily it's cheap, it's only $11 total. I just hate to buy another book with only 2 more chapters to go, but what else can I do?
 
Last edited:
  • #25
yungman said:
One gain knowledge by keep experimenting.

One also gains knowledge by listening to the answers to the questions one asks.
 
  • #26
Vanadium 50 said:
One also gains knowledge by listening to the answers to the questions one asks.
When you actually answer my questions, I'll listen.
 
Last edited:
  • #27
yungman said:
When you actually answer my questions, I'll listen.
I looked into it a little. There should be a place to add "additional include directories" in the project properties settings. You can probably find it by just looking around in the settings available for your VS project. When you find that, you add your directories to the list. Note this is normally used in VS for external files, that your project uses but you aren't working on within the project.
 
  • Like
Likes yungman
  • #28
yungman said:
When you actually answer my questions, I'll listen.

You know, I'd show a little more gratitude and a little less attitude towards the people who are graciously donating their time to help you.
 
  • Like
Likes phinds
  • #29
Vanadium 50 said:
You know, I'd show a little more gratitude and a little less attitude towards the people who are graciously donating their time to help you.
I actually show a lot of gratitude to anyone that actually help me Like Mark, Jarvis, factchecker, pbuk, Sysprog, Phinds and others. I can't thank them enough to help me through this hard time in learning. I always thank them in every post they answered me.
 
Last edited:
  • #30
Jarvis323 said:
I looked into it a little. There should be a place to add "additional include directories" in the project properties settings. You can probably find it by just looking around in the settings available for your VS project. When you find that, you add your directories to the list. Note this is normally used in VS for external files, that your project uses but you aren't working on within the project.
Thanks for giving me a push to look into this more. Using Project-->Add Class works.
It will open a window like this. I just typed in RectConstr in the Class name. It will fill the name for you in .h and .cpp as shown. You then click OK.
Compile error Listing 7.2.jpg
It will open a blank project with empty file RectConstr.h and RectConstr.cpp on the right side as shown below:
Compile error Listing 7.3.jpg


I click RestConstr.h and paste the code of my RestConstr.h in, then do the same for RestConstr.cpp. It work one time through.
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.
Compile error Listing 7.4.jpg

Notice I have to put line 3 to point to the external file to make it work. I put in the EXACT same file shown by the arrow on the left that it's there. But if I just put "Rectangle.h", it will NOT work.

Like I said, I don't know enough to say why, it just is.

Thanks for giving me a push to look deeper into VS to find another way to link the files. Not only that, my issue of Post #24 is resolved using this new method.

Thanks
 
  • #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.
 
  • #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?
 
Back
Top