I recently began learning C++. As a programmer coming from Python, I've noticed some general similarities when it comes to how certain things in C++ do the same thing over in Python.
One question I had is understanding Preprocessor directives. I/O Stream seems to be a common one to use in beginner programs.
Is #include effectively the same thing as import in Python, or is it completely different than importing "modules"?
C++ did not have modules until the latest standard (C++20). #include is not the same as import in the languages that support modules. Instead, it is a source code - level inclusion of a "header" file. Usually, the headers only contain declarations but not definitions of what you are "importing". The definitions are contained in compiled libraries that are added by the linker.
Congrats on diving in to C++, you're going to have many more questions and confusions coming from Python, especially if you use some of the newer standards (like C++11/14/17/20).
That aside, answering your question directly:
Is #include effectively the same thing as import in Python or is it completely different than importing "modules."
I won't speak to C++20 modules as that functionality is not fully supported across the various compilers and that is not your question. Unfortunately the answer is not a simple yes or no, it's kind of both.
In C and C++, the #include pre-processor directive essentially does a "copy-paste" of whatever file you #include before it does the compilation stage. This allows you to separate large chunks of code into easier to manage files and still reference the code in said file.
In Python/C#/Java and various other languages, you don't #include a file you want to access the classes and functions of, you import the namespace or module you wish to reference and the JIT compiler "knows" which file that module or namespace is in, allowing you to use the functionality of the code in that file.
Python and C++ don't "build" the code in the same way and thus don't reference parts of the source code in the same way.
To illustrate this point more succinctly, take the following C++ code:
file: fun.hpp
#define FUN_NUM 1
namespace fun
{
int get_fun()
{
return FUN_NUM;
}
}
file: main.cpp
#include <iostream>
#include "fun.hpp"
int main(int argc, char* argvp[])
{
if (fun::get_fun() == FUN_NUM) {
std::cout << "Fun!" << std::endl;
}
return FUN_NUM;
}
In the above code, when we #include "fun.hpp", what the C++ pre-processor does before compiling is essentially "copy-and-paste" the code in iostream and fun.hpp, so what actually gets compiled is something like the following:
file: main.cpp
// #include <iostream> <- this is replaced with the whole std::iostream file
// not putting that here as it's huge.
// #include "fun.hpp" <- this is replaced with this:
#define FUN_NUM 1
namespace fun
{
int get_fun()
{
return FUN_NUM;
}
}
int main(int argc, char* argvp[])
{
if (fun::get_fun() == FUN_NUM) {
std::cout << "Fun!" << std::endl;
}
return FUN_NUM;
}
It is because of this "copy-paste" that you also need to have include guards, because if you did something like the following:
file: main.cpp
#include <iostream>
#include "fun.hpp"
#include "fun.hpp"
int main(int argc, char* argvp[])
{
if (fun::get_fun() == FUN_NUM) {
std::cout << "Fun!" << std::endl;
}
return FUN_NUM;
}
This code won't compile because you'll get errors about various items being redeclared since what gets compiled is the following:
file: main.cpp
// #include <iostream> <- this is replaced with the whole std::iostream file
// not putting that here as it's huge.
// #include "fun.hpp" <- this is replaced with this:
#define FUN_NUM 1
namespace fun
{
int get_fun()
{
return FUN_NUM;
}
}
// #include "fun.hpp" <- this is replaced with this:
#define FUN_NUM 1
namespace fun
{
int get_fun()
{
return FUN_NUM;
}
}
int main(int argc, char* argvp[])
{
if (fun::get_fun() == FUN_NUM) {
std::cout << "Fun!" << std::endl;
}
return FUN_NUM;
}
To protect from the double inclusion and redefinition, you can simply do something like the following:
file: fun.hpp
#if !defined(FUN_HPP)
#define FUN_HPP
#define FUN_NUM 1
namespace fun
{
int get_fun()
{
return FUN_NUM;
}
}
#endif // define FUN_HPP
So unless you pass FUN_HPP as a pre-processor define to the compiler, then FUN_HPP will not be defined until the file is #include'd once, then any other times it's included, FUN_HPP will already be defined and thus the pre-processor will not include the code again, ridding the problem of double-definitions.
So where your question is concerned, the #include directive in C++ is somewhat like the import directive in Python, but mostly to the effect that they both allow the file you are putting that directive in, to access code more directly from that import or #include.
I hope that can add a little clarity.
When I include the required library, the "#include.." line doesn't show any warning. But when I use the functions in that library, I find the Vim shows that "..use of undeclared function...". It seems that the library is not correctly included. So I want to know how to figure out this problem?
The screenshots for this question are attached as follows:
Try including it as follows:
#include <stdlib.h> //use <> instead of ""
Also, the "printf" function comes from the "cstdio" library so try implementing that library as well,
#include <stdio.h>
UPDATED
The easiest way to fix that problem is;
Include the stdio.h library
#include <stdio.h>
Then, instead of typing;
printf('s');
you do,
printf("s");
Now, if you really want to print a character 's', then use,
printf("%c", 's'); // Tells the printf function that 's' is a character
The final code would look like;
#include <stdio.h>
int main(int argc, char** argv) {
printf("s");
printf("%c", 's');
return 0;
}
Now, your comment was that "cout" does not work. In order for "cout" to work you need to include the iostream library:
#include <iostream>
Then, you can use "cout" in your code;
std::cout << 's';
std::cout << "s";
Or you can include "namespace std" and the "iostream" library to avoid using std:: before "cout"
include <iostream>
using namespace std;
Thereafter, use cout without std::
cout << 's';
cout << "s";
The final code would be;
#include <iostream>
using namespace std;
int main(int argc, char** argv) {
cout << 's';
cout << "s";
return 0;
}
If you want to learn more about what is in the iostream library and how to use it I recommend using this site:
http://www.cplusplus.com/reference/iostream/
Also, for the stdio.h,
http://www.cplusplus.com/reference/cstdio/
probably a stupid question but I can't get this to work for some reason. I'm quite new to C++ so I think I might have some misunderstanding somewhere.
graphics.hpp:
#ifndef Airport_game_hpp
#define Airport_game_hpp
#include <SDL2/SDL.h>
namespace graphics {
namespace gl{
static SDL_Window *mainWindow;
static SDL_GLContext mainContext;
bool initGL();
bool destroyGL();
}
}
#endif
game.hpp:
#ifndef Airport_game_hpp
#define Airport_game_hpp
namespace game{
void render();
}
#endif
main.cpp:
#include <iostream>
#include "graphics/graphics.hpp"
#include "game/game.hpp"
int main(int argc, char* argv[])
{
std::cout << "Starting application \n";
if (!graphics::gl::initGL()){
std::cout << "OpenGL initialization failed \n";
return false;
}
//Test
game::render(); //This line says: Use of undeclared identifier "game"
graphics::gl::destroyGL();
std::cout << "Exit successful \n";
}
If I swap the order of the #include-s on main.cpp the "game" namespace is seen but not the "graphics" one. It seems like it only sees one at a time. What am I misunderstanding here?
Thanks!
You're using the same include guard in both headers. Ideally the include guard should reflect the name of the module or header, but you should at least change one of them, in order to make them both unique.
Both headers use the same include guard: Airport_game_hpp. This means that the second header to be included will be ignored, since its guard has already been defined by the first.
Change the guard for graphics.hpp to Airport_graphics_hpp and, in general, make sure your guard names are unique.
#include <cstdlib>
using namespace std;
/*
*
*/
int main(int argc, char** argv)
{
cout << "COME AT ME BRO!\n"
return 0;
}
It says cout is unable to resolve identifier
The C++ code assistance is setup properly, I'm just not sure whatelse it could possibly be.
You did not include <iostream> and thus the identifier std::cout is never declared or defined in your program.
You're including the wrong header file. It should be :
#include <iostream>
I am working on the 'driver' part of my programing assignment and i keep getting this absurd error:
error C2065: 'cout' : undeclared identifier
I have even tried using the std::cout but I get another error that says:
IntelliSense: namespace "std" has no member "cout"
When I have declared using namespace std, included iostream and I even tried to use ostream
#include <iostream>
using namespace std;
int main () {
cout << "hey" << endl;
return 0;
}
I'm using Visual Studio 2010 and running Windows 7. All of the .h files have using namespace std and include iostream and ostream.
In Visual Studio you must #include "stdafx.h" and be the first include of the cpp file. For instance:
These will not work.
#include <iostream>
using namespace std;
int main () {
cout << "hey" << endl;
return 0;
}
#include <iostream>
#include "stdafx.h"
using namespace std;
int main () {
cout << "hey" << endl;
return 0;
}
This will do.
#include "stdafx.h"
#include <iostream>
using namespace std;
int main () {
cout << "hey" << endl;
return 0;
}
Here is a great answer on what the stdafx.h header does.
write this code, it works perfectly..
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
cout<<"Hello World!";
return 0;
}
I had same problem on Visual Studio C++ 2010. It's easy to fix. Above the main() function just replace the standard include lines with this below but with the pound symbol in front of the includes.
# include "stdafx.h"
# include <iostream>
using namespace std;
The include "stdafx.h" is ok
But you can't use cout unless you have included using namespace std
If you have not included namespace std you have to write std::cout instead of simple cout
If the only file you include is iostream and it still says undefined, then maybe iostream doesn't contain what it's supposed to. Is it possible that you have an empty file coincidentally named "iostream" in your project?
I have seen that if you use
#include <iostream.h>
then you will get the problem.
If you use
#include <iostream>
(notice - without the .h)
then you will not get the problem you mentioned.
If you started a project requiring the #include "stdafx.h" line, put it first.
I've seen similar things happen when I was using the .c file extension with C++ code. Other than that, I'd have to agree with everyone about a buggy installation. Does it work if you try to compile the project with an earlier release of VS? Try VC++ Express 2008. Its free on msdn.
Such a silly solution in my case:
// Example a
#include <iostream>
#include "stdafx.h"
The above was odered as per example a, when I changed it to resemble example b below...
// Example b
#include "stdafx.h"
#include <iostream>
My code compiled like a charm. Try it, guaranteed to work.
The code below compiles and runs properly for me using gcc. Try copy/pasting this and see if it works.
#include <iostream>
using namespace std;
int bob (int a) { cout << "hey" << endl; return 0; };
int main () {
int a = 1;
bob(a);
return 0;
}
I have VS2010, Beta 1 and Beta 2 (one on my work machine and one at home), and I've used std plenty without issues. Try typing:
std::
And see if Intellisense gives you anything. If it gives you the usual stuff (abort, abs, acos, etc.), except for cout, well then, that is quite a puzzler. Definitely look into your C++ headers in that case.
Beyond that, I would just add to make sure you're running a regular, empty project (not CLR, where Intellisense is crippled), and that you've actually attempted to build the project at least once. As I mentioned in a comment, VS2010 parses files once you've added an include; it could be that something stuck the parser and it didn't "find" cout right away. (In which case, try restarting VS maybe?)
Take the code
#include <iostream>
using namespace std;
out of your .cpp file, create a header file and put this in the .h file. Then add
#include "whatever your header file is named.h"
at the top of your .cpp code. Then run it again.
I had the same issue when starting a ms c++ 2010 project from scratch - I removed all of the header files generated by ms and but used:
#include "stdafx.h"
#include <iostream>
using namespace std;
int main() {
cout << "hey" << endl;
return 0;
}
I had to include stdafx.h as it caused an error not having it in.
Try it, it will work. I checked it in Windows XP, Visual Studio 2010 Express.
#include "stdafx.h"
#include <iostream>
using namespace std;
void main( )
{
int i = 0;
cout << "Enter a number: ";
cin >> i;
}
before you begin this program get rid of all the code and do a simple hello world inside of main. Only include iostream and using namespace std;.
Little by little add to it to find your issue.
cout << "hi" << endl;
Are you sure it's compiling as C++? Check your file name (it should end in .cpp). Check your project settings.
There's simply nothing wrong with your program, and cout is in namespace std. Your installation of VS 2010 Beta 2 is defective, and I don't think it's just your installation.
I don't think VS 2010 is ready for C++ yet. The standard "Hello, World" program didn't work on Beta 1. I just tried creating a test Win32 console application, and the generated test.cpp file didn't have a main() function.
I've got a really, really bad feeling about VS 2010.
When you created your project, you did not set 'use precompiled headers' correctly. Change it in properties->C/C++->precompiled headers.
In Visual studio use all your header filer below "stdafx.h".
Just use printf!
Include stdio.h in your stdafx.h header file for printf.
Include the std library by inserting the following line at the top of your code:
using namespace std;
is normally stored in the C:\Program Files\Microsoft Visual Studio 8\VC\include folder. First check if it is still there. Then choose Tools + Options, Projects and Solutions, VC++ Directories, choose "Include files" in the "Show Directories for" combobox and double-check that $(VCInstallDir)include is on top of the list.
I ran across this error after just having installed vs 2010 and just trying to get a nearly identical program to work.
I've done vanilla C coding on unix-style boxes before, decided I'd play with this a bit myself.
The first program I tried was:
#include "stdafx.h"
int _tmain(int argc, _TCHAR* argv[])
{
cout << "Hello World!";
return 0;
}
The big thing to notice here... if you've EVER done any C coding,
int _tmain(int argc, _TCHAR* argv[])
Looks weird. it should be:
int main( int argc, char ** argv )
In my case I just changed the program to:
#include <iostream>
using namespace std;
int main()
{
cout << "Hello world from VS 2010!\n";
return 0;
}
And it worked fine.
Note: Use CTRL + F5 so that the console window sticks around so you can see the results.
I came here because I had the same problem, but when I did #include "stdafx.h" it said it did not find that file.
What did the trick for me was: #include <algorithm>.
I use Microsoft Visual Studio 2008.
These are the things that you can use then, incl. 'count': Link
Had this problem, when header files declared "using namespace std;", seems to be confusing for GNU compiler;
anyway is bad style!
Solution was providing std::cout ... in headers and moving "using namespace std" to the implementation file.
In VS2017, stdafx.h seems to be replaced by pch.h see this article,
so use:
#include "pch.h"
#include <iostream>
using namespace std;
int main() {
cout << "Enter 2 numbers:" << endl;
It was the compiler - I'm now using Eclipse Galileo and the program works like a wonder