• Welcome to Overclockers Forums! Join us to reply in threads, receive reduced ads, and to customize your site experience!

Include files in C++ (nooblar)

Overclockers is supported by our readers. When you click a link to make a purchase, we may earn a commission. Learn More.

Bigdogbmx

Member
Joined
May 17, 2003
Location
England-Leeds
HI, I just started to learn C++ and every example program code I see has 'include' files. I understand teh concept but I dont know if I actually have these files because every time I try to compile some code I have seen in a tutorial an error comes up. No matter which tutorial and from where. I have Bloodshed IDE and have set up the include files like In frodos lessons in the sticky at the top of this forum. Should the program just auto use the ones in the IDEs directory or do I need to download or write these include files and place them somewhere?
TIA for any answers :)
 

Titan386

Senior Member
Joined
Jun 8, 2002
Dev-C++ should come with header files (or more accurately, it should come with the include files of the Mingw project). If you look around in your Dev-C++ directory, you should see an include directory, and inside that directory should be your header files for the standard library and such. You might want to verify that you're telling the compiler to use that directory as an include directory.

If you're still having trouble, try compiling the following file:
Code:
#include <iostream>

int
main (int argc, char **argv)
{
       std::cout << "Hello World!\n";
       return 0;
}

If that doesn't work, please paste the error message here and we can try to sort out your problem.
 
OP
Bigdogbmx

Bigdogbmx

Member
Joined
May 17, 2003
Location
England-Leeds
Ok the code you posted compiled fine :D
Also I checked the C:\dev-cpp\include dir and there were a shedload of files in there so thats ok i guess. Does this just mean instead of say #include <conio.h>, i need to be putting #include <C:\dev-cpp\include\conio.h> ?
 

Titan386

Senior Member
Joined
Jun 8, 2002
No, the compiler should "know" to look in that directory for the header files. The directories the compiler looks in are called include directories, so in your case, you would want C:\dev-cpp\include to be one of your include directories.

If the stuff I posted works, then your include directories should be set correctly. If you have further problems, please post the names of the header files you're trying to use and the errors you get.

BTW, the reason why the responsiblity to find the proper location of the header files is given to the compiler and not to the program being compiled is to allow for portable programming. For example, your iostream is in C:\dev-cpp\include\, but on my Linux system, it resides in /usr/include. Quite a difference :) As long as my compiler knows where to find its iostream, a program you've written should compile fine on my machine.
 
OP
Bigdogbmx

Bigdogbmx

Member
Joined
May 17, 2003
Location
England-Leeds
I see now thats pretty clever, someones obviously thought about this lol. I get file not found error now for iomanip.h now though. Even though I can see a file called iomanip in my include\c++ directory and i have it set to use that directory as one of the 'C++ includes' ones. Also I get these
4 C:\Dev-Cpp\test2.cpp
`main' must return `int'
C:\Dev-Cpp\test2.cpp
[Warning] In function `int main(...)':

6 C:\Dev-Cpp\test2.cpp
`cout' undeclared (first use this function)
7 C:\Dev-Cpp\test2.cpp
`cin' undeclared (first use this function)
8 C:\Dev-Cpp\test2.cpp
`endl' undeclared (first use this function)
The thing I dont get there is that I thought cout and cin etc were all kind of preset functions. Like in Javascript when you use onmouseover the browser knows what to do. I assumed the compiler would also know wat to do. Could you tell me what is wrong to cause this?
 

Titan386

Senior Member
Joined
Jun 8, 2002
If the file is called iomanip, then you must use
#include <iomanip>
not
#include <iomanip.h>

cin, cout and friends are part of the C++ standard library. However, they live inside something called a namespace, which is basically a way of seperating and partitioning functions and such so that they do not interfere with each other.

For example, let's say that I decide that I want to use a variable called cout, as well as using the cout you're familar with, for output. The compiler would be confused, because it has no way of knowing which cout I want to use where. Namespaces provide a solution to this problem, by forcing you to append a string called a namespace at the beginning of functions and variables that exist in another namespace.

That might be rather confusing, so here's an example:

Code:
#include <iostream>

int
main (int argc, char **)
{
    int cout = 3;
    std::cout << "cout = " << cout << std::endl;
    return 0;
}

Notice how I appended std:: to both cout and endl. I did that because they both exist in the namespace called std. This allows my integer variable, cout, to coexist with the cout of the C++ standard library. So, in order to use a function or a variable from another namespace, you must append the name of the namespace and two colons to the name of whatever your trying to access.

If you're lazy, you can use a so-called using directive to remove the need to give the namespace, but only for one namespace at a time. For example, this is one way of writing the Hello World program:

Code:
#include <iostream>

using namespace std;

int
main (int argc, char **argv)
{
    cout << "Hello world!" << endl;
    return 0;
}

This time, I didn't need the std:: in front of cout, because I already told to compiler to look in the std namespace, using the line:
using namespace std;

Personally, I feel that using directives defeat the whole purpose of namespaces and shouldn't be used much. But that's just me... :)
 
OP
Bigdogbmx

Bigdogbmx

Member
Joined
May 17, 2003
Location
England-Leeds
Ooooooooooooh I get it :D Thank you a LOT titan386! Things like this are what OC forums are so good for. Not many places someone will answer your nooblar questions straight away and give u a good explanation as well. 👍
 

Titan386

Senior Member
Joined
Jun 8, 2002
Glad to be of help :)
Feel free to let me know if you have any other questions.
 
OP
Bigdogbmx

Bigdogbmx

Member
Joined
May 17, 2003
Location
England-Leeds
More problems funnily enough :rolleyes:
I used the std thing before every cout or cin. Well heres the code I tried to compile anyway.
#include <iomanip>
#include <conio.h>
void main()
{
int age;
std::cout <<"Hello, Im a pointless program that says dumb stuff when you answer stupid questions"<<std::endl;
std::cout<<"Ok first stupid question. How old are you?"<<std::endl;
std::cin>>age;
{if (age==0)
std::cout<<"You Lie like a common theif"<<std::endl;}
{if (age<=10)
std::cout<<"Alright young guns. You know something, SANTAS NOT REAL! Mwahaha, you wish you hadnt tried this program out"<<std::endl;}
{ if (age<=18)
std::cout<<"Put that beer down and go back to school. And no you cant have one with your christmas dinner you cheeky bugger"<<std::endl;}
{ if (age>=60)
std::cout<<"Good on you for keeping up with all this computer business, If I was you age Id be sat in a chair with a pipe moaning and not playing with computers."<<std::endl;}
}

And heres the errors
4 C:\Dev-Cpp\age test.cpp
`main' must return `int' C:\Dev-Cpp\age test.cpp
[Warning] In function `int main(...)':
7 C:\Dev-Cpp\age test.cpp
`cout' undeclared in namespace `std'
8 C:\Dev-Cpp\age test.cpp
`cin' undeclared in namespace `std'
Obviously the 'cin undeclared in namespace std' error showed for every time i used cin or cout but I didnt bother pasting it every time. Does this mean the compiler still thinks I want to use cout and cin as variables?