Hello all,

So to start off I'm just starting to learn programming so please excuse a lot of my noob mistakes especially when trying to explain what is going on/wrong.

I've been trying to use a header file in an example program I'm trying to complete but the compiler doesn't recognize the file...in fact it says that no such file or directory exists. I'm using Visual C++ 2008 Express Edition if that makes a difference on how to set anything up.

So I was wondering if anyone could walk me through the process of setting up a header file. Or a link to the same effect so I know that I'm starting the process correctly. Thanks!

Well, if you've created a new C++ project/solution, there should be a Solution Explorer visible somewhere on the screen. I believe there's a 'File View' which can be toggled. When that is selected you should see a Tree View Control that lists the various file types you can include in a project. Naturally, C++ projects need a *.cpp file. These will be listed under the 'Source' files node. Under the 'Header' files node are the header fies in your project if there are any. If there aren't then Right Clicking on that node will pop up a context menu where you can either navigate to where some *.h file exists that you want to include in your project, or give you the option of adding a new blank file into which you can type header file type info.

Is this making any sense? Have you gotten that far?

If you have something like this...

#include <iostream.h>
#include "MyHeader.h"

...and the compiler error says it can't find "MyHeader.h", then that means the compiler can't find "MyHeader.h". In other words, if its not in the directory its looking, you'll have to add a explicit path to the file, or put it in with the other source files for the project where the compiler wil find it.

Edited 7 Years Ago by Frederick2: n/a

Hi, ExMachina.

You have to write this:

#include<header_name.h>

(ANGLE BRACKET: This form instructs the preprocessor to search for include files first along the path specified by the /I compiler option, then, when compiling from the command line, along the path specified by the INCLUDE environment variable.)

or

#include"header_name.h"

(DOUBLE QUOTATION MARKS: This form instructs the preprocessor to search in the same directory of the source files.)

Keep in mind that in new C++ standard, the header files of the standard library haven't the .h extension. So, you have to write <iostream> instead of <iostream.h>.

---------------------------------------------------------------
In Visual C++ you have to create a project file to compile the sources (even if the source code consist of a single file).

Edited 7 Years Ago by Bezelis: n/a

Frederic2,

Thank you for the input thus far. In the explorer window I have the header file under the "Headers" node and the .cpp source code under the "Source" node and I have the code starting with

#include <iostream>
#include "GradeBook.h"

but when I try to build the solution it still comes up with fatal error C1083: Cannot open include file: 'GradeBook.h' : No such file or directory.

And after reading what you posted I moved the .h file into the same directory as the 'GradeBook' program but that didn't help.

Bezelis,

Also just wanted to thank you for your input as well, although I'm kinda lost with what you're talking about. Granted I haven't tried to compile anything from the command line yet....although I need to practice that at some point soon! I'm a little confused with what you said concerning the DOUBLE QUOTATION MARKS and about the standard library not having the .h extension and having to write <iostream> instead of <iostream.h>. If you could explain that a little more indepth I would appreciate it.

Indeed I have. And when its just a .cpp project (minus all my syntax errors) they have all compiled and ran just like they were supposed to. but when I went to create the header file I went File-New-File... instead of File-New-Project (figuring that is how you create a header file).

Well, that's good! Then we know your setup is working.

Did you try specifying the exact path in the #include line, such as...

#include "C:\Documents and Settings\VStudio\MyRecalcitrantHeader.h"

I mean, you shouldn't have to do that, but I'm just trying to figure out what's wrong, i.e., why it won't read a file you claim is there?

Edited 7 Years Ago by Frederick2: n/a

GradeBook... Is it "C++ How to program (Deitel & Deitel)", isn't it?

First of all, I'm sorry for my bad language (I'm not English).

I had understood you had some problem with the inclusion of an header file.

Visual Studio allow you to compile only the source code of a project, because in most cases the code is complex and it is subdivided into more files. The other compilers, such as Borland C++ and GCC, can immediately compile a single file. Then, in VC++ you have to click on "File --> New --> Project --> Visual C++ --> General --> Empty Project" and create a solution. After this, you have to click with the right button of the mouse in "Source File --> Add" to load the C++ files. And in "Header File --> Add" to load YOUR header files, NOT the STANDARD HEADER FILE (iostream, cstdlib, etc.).

However, in C++ there are some conventions to include an header file. The name of the header file between angle brackets < > tell to the compiler to search it in the standard directories, defined by the compiler itself (the standard header files, such as "iostream", are contained in one of this directories). The name of the header file contained in the double quotation marks " " tell to the compiler to search in the directories wherein there is the source code.

What are "Standard Header Files"? They are the header files of the Standard Library. The C++ Language doesn't define functions (i.e. math functions, i/o functions, etc.), so the ANSI (American National Standards Institute) and the ISO nominated programmers to write some common and useful functions for C++. These functions were included into the standard library. For example, "iostream" contains Input/Output functions. The Standard Library (derived from C language), is attached to compilers.

http://en.wikipedia.org/wiki/C_standard_library

In the old C++ revision, the name of standard header files had an extension .h. In the new revisions, this extension was removed. So, there are some compiler that accept only iostream.h and other (modern compiler) that accept only iostream.

Take a look at MSDN for compiler errors:
http://msdn.microsoft.com/en-us/library/et4zwx34%28VS.80%29.aspx

Edited 7 Years Ago by Bezelis: n/a

Comments
excellent material on topic

Frederick2,

So when I copy and pasted the entire path ie. "C:\Documents and Settings...myHeaderFile.h" the program compiled with ZERO problems!!! I am quite curious now because I didn't think that I had to put all of that in the #include " *.h". Any suggestions?

Bezelis,

Thank you for the in depth explanation!! That helped me understand a lot more of the logic of programming in C++ and yes you are right in that "GradeBook" is a Dietel project. Oh and thank you for the links!!! If you have any suggestions as to why I would have to indicate the entire path for my header file to work I would welcome the input (because right now i have to put #include "C:\Documents and Settings\Visual Studio 2008\Projects\myHeaderFile.h" for the program to compile.)

No, I don't know why you would have to do that, ExMachina. But it was a good idea wasn't it!

Any chance you installed Visual Studio under one log in and are developing under another? Possible rights problems? Just grasping at straws here!

If you copied that file from a CD is it marked as read only??? That ones bit me a few times. As the other knowlegeable fellow said, the development environment has several places it looks for things before crapping out with an error.

I actually just bought VC9 myself recently. All these modern compilers seem to be creating ever more complicated directory setups for a project's files, that's for sure. About my favorite is Dev-cpp. With that I can get a project up and running in about 30 seconds. I can't say that for any of the others, i.e., CodeBlocks, VC6, VC9, etc.

You're welcome! :)

Yes, you can enter the entire path of your header file, but you're not obligated to do that. If you put the header file and the source file in the same directory, you can enter only the name of the header file. Here an example (Please, follow my instructions).

1. Create a directory in the desktop, called "Pythagoras".

2. Open a text editor (NoteBook or other) and create two files into it, rispectively MySource.cpp and MyHeader.h. Remember to save the correct extension with the text editor (not TXT extension).

MyHeader.h

#include<cmath>

float ExMachina(float x, float y);

float ExMachina(float x, float y)
{
   float hyp = sqrt(x*x + y*y);
   return hyp;
}

In the previous code, we are defined the function ExMachina(). <cmath> containing the function sqrt(). ExMachina() take in input two arguments and solve the Pythagorean Theorem.

MySource.cpp

#include<cstdlib>
#include<iostream>
#include"MyHeader.h"
using namespace std;

int main()
{
    float cath1, cath2;
	float hypotenuse;

	cout << "Insert a number (cathetus 1): ";
	cin >> cath1;
	cout << "Insert a number (cathetus 2): ";
	cin >> cath2;

	hypotenuse = ExMachina(cath1, cath2);

	cout << endl << "The hypotenuse is: " << hypotenuse << endl << endl;

	system("pause");
	return 0;
}

3. Open Visual C++ and click on "File --> New --> Project --> General --> Empty Project". Insert a project name and click OK.

4. Drag your file MySource.cpp in the "Source Files" (in VC++) and MyHeader.h in the "Header Files".

5. Click on the entry "Build".

Here, the project file: http://www.megaupload.com/?d=VVI0TN9W


Ciao,
Angelo

Edited 7 Years Ago by Bezelis: n/a

Another thing. People usually begins to learn computer science starting from an example program like this: Please, write a program to display the string "hello world, ciao mondo, bonjour le monde" and sum 5 + 3.

In my honest opinion, it is better to think about the problem to solve before attempting to transform it into a program. Here's how it should be the steps:

1. Problem (informal)
Example: I have to go from Rome to London

2. Problem (formal)
I write all the possible routes to go from Rome to London and I study them. (Graph Theory)

3. Algorithm (mathematics / physics)
I elaborate an algorithm (What is it the fastest route? Or, what is it the shortest route?)....

Curiosity (Dijkstra's Algorithm)

4. Program (computer languages)

People usually follows this steps in reverse.

Edited 7 Years Ago by Bezelis: n/a

Frederick2,

Yeah that was a good idea!!! And to answer your other questions I'm logged in under the same profile as I loaded the program and it isn't write protected (at least as far as I know...I will have to do some more digging just to be sure) but thank you for all your input!

Bezelis,

Seriously thank you for all the help...especially with your last post on how to approach writing a program. That's going to take some work to change the mindset but it will really help. Thanks! And as soon as I get the chance I will try that program and let you know how it turns out!!!

You know, sometimes, when things don't work for seemingly no reason at all, it pays to just start over, reboot, whatever. In your case ExMachina, have you considered just deleting the project (saving the source, of course) and starting over? There's almost certainly a bad reference involved somewhere that is messing things up - perhaps something you did in setting up the project. Before doing that perhaps you could try the less drastic solution of just highlighting the misbehaving include file in the solution explorer, and hitting the delete key. A popup will ask if you want to delete the file or simply remove it from the project. Just tell it to remove it. Then shut down VS and restart. Open up the project and re-include the file. Right click on the 'Headers' folder and select 'Add Existing Item', then try again. It might work.

This article has been dead for over six months. Start a new discussion instead.