1.11M Members

error of header files in codeblocks

 
0
 

While compiling a simple code in c++ i get the errors
C:\Users\ila\Desktop\mule\mule.c|1|iostream: No such file or directory|
C:\Users\ila\Desktop\mule\mule.c||In function `main':|
C:\Users\ila\Desktop\mule\mule.c|6|error: `cout' undeclared (first use in this function)|
C:\Users\ila\Desktop\mule\mule.c|6|error: (Each undeclared identifier is reported only once|
C:\Users\ila\Desktop\mule\mule.c|6|error: for each function it appears in.)|
||=== Build finished: 4 errors, 0 warnings ===|
How do I include header files in c++?

 
0
 

Probably differently than you did in your program. But since you didn't post any code, we can't tell what you did wrong.

 
0
 

the code was this

#include <stdlib>
#include <iostream>
#include <conio>
int main(){
cout<<"\n hello";
return 0;
}

 
0
 

The thing you're forgetting here is that cout is a member of the std:: namespace, so you either need to explicitly resolve that cout is part of the std:: namespace by specifying std::cout every time you use it e.g.

std::cout << "\nhello";

Otherwise, to avoid having to explicitly resolve the namespace each time you use cout you can use a using statement. e.g.

#include <iostream>

using std::cout;

int main()
{
    cout << "\nHello";
    return 0;
}

Doing this will mean that you can use cout << "whatever"; rather than having to type std::cout << "whatever"; each time!

A lot of people also use using namespace std; This exposes the entire std:: library's namespace to your application, so you can use any of the std::library classes without having to qualify them with std:: e.g.

#include <iostream>
#include <string>

using namespace std;

int main()
{
    string msg("\nHello");
    cout << msg << endl;
    return 0;
}

The above snippet uses std::string, std::cout and std::endl, but because we used using namespace std; , we don't need to resolve them.

Doing this is completely fine if you're using nothing but the std:: library in your program. But if you are using several libraries/namespaces, you need to be aware that some libraries could potentially contain classes with the same names as classes in other libraries/namespaces you are using. In which case, you run the risk of encountering namespace clashes/resolution errors. So the using namespace keyword should be used carefully!

Generally speaking, AFAIK it is considered good practice to add 'using' statements to resolve only the classes you're using in your program instead of using using namespace to resolve an entire namespace.
e.g.

#include <iostream>
#include <string>

using std::cout;
using std::string;
using std::endl;

int main()
{
    string str("\nHello");
    cout << str << endl;
    return 0;
}

This reduces the risk of namespace clashes/resolution errors in your programs.

Otherwise, if you explicitly resolve all classes you can eliminate the risk of namespace clashes/resolution errors entirely!
e.g.

#include <iostream>
#include <string>

int main()
{
    std::string msg("\nHello");
    std::cout << str << std::endl;
    return 0;
}

Whether you choose to use using std::cout; , or using namespace std; or to explicitly resolve each time (e.g. std::cout << "Something" << std::endl; ) is a matter of personal coding style and is completely up to you. But you should at least be aware of the issues I have mentioned with regard to the using namespace keyword. Especially if you ever start using several libraries/namespaces in your programs!

 
0
 

I notice your source file is called mule.c. Most likely your compiler is automatically detecting the desired language from the source file extension, which means you're not compiling as C++ but as C. C doesn't support the <iostream> header.

Try forcing the compiler to compile as C++, or change the extension to *.cpp or *.cc and see if that fixes the problem. Note that you'll still have to add code to qualify for namespace std as described by JasonHippy.

 
0
 

BTW: Regarding the error where the compiler was complaining about not being able to find iostream, #include <iostream> may just need to be put at the top of your list of #includes.

Although that said, <stdlib> and <conio> are old C headers, they don't exist in C++ (well, they do, but not as <stdlib> or <conio> as I'll explain shortly!).

The very fact that the compiler is complaining that <iostream> cannot be found and at the same time isn't complaining about <stdlib> and <conio> not being found; leads me to believe that you may have set up a C project in Code::Blocks rather than a C++ project, which could also explain your problems! Are you 100% sure that you're working with a C++ project in C::B??

To check, you could try using the new project wizard in C::B to create a new C++ console project and then either paste your code in the generated main.cpp (but don't forget to qualify std::cout as outlined in my previous post!) or paste one of the snippets from my previous post into main.cpp and see how you get on.

If you do need to use any of the old C functions in stdlib and/or conio in a C++ project you'd typically need to use:

#include <cstdlib> // correct C++ header for the C stdlib.h
#include <conio.h> // uses the old C header, but requires .h at the end!

Also you should be aware that stdlib (or cstdlib) and conio are not needed in your posted code. iostream is the only header required for the simple std::cout you posted!

So to round up... From the looks of the error messages and your posted code; I could be mistaken, but it looks to me that you may have inadvertantly set up a C project rather than a C++ project in C::B!

Also note: Everything in my previous post regarding namespace resolution still stands!

<EDIT:> Oops... beaten to it by Narue! Damn my long-winded wordiness! heh heh! :)
Hadn't noticed the file extension in the ops posts either... Confirms my suspicions though! Thanks Narue!

You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article