C++ programming not compiling <iostream> - c++

I'm beginning to learn C++ programming, I'm using the visual studio editor.
This is the simple code I entered:
#include <stdio.h>
#include <iostream>
using namespace std;
int main(){
cout << "Hello, comply";
return 0;
}
I only added the "#include iostream" because my textbook says that is needed to let the program output to screen. I tried to compile and run in my command prompt and its giving me some error:
**C:\Users\edika\Desktop>gcc comply.c -o comply.exe
comply.c:2:22: fatal error: iostream: No such file or directory
#include <iostream>
^
compilation terminated.**
What am I doing wrong?

you're using a C compiler for C++. Additionally, your file extension should be *.cpp to indicate C++ code.
You're using gcc, you need g++.
Install G++, change your file name to comply.cpp, then run "g++ comply.cpp -o comply.exe"

Related

Using "string" header file gives error in c++ on Windows 10

So I recently upgraded Windows 8.1 to Windows 10. I didn't faced this type of error before but when I program in c++ and include "string" header file then it pops out an error when executing though compiling does not give any error. I tried using "cstring" and update Microsoft Visual C++ Redistributable but no change. If I don't use "string" header file then it will not produce any error.
Here is test.cpp
#include <iostream>
#include <string>
using namespace std;
int main(){
string text = "Hello World!";
cout << text;
return 0;
}
On compiling it does not give error
g++ test.cpp -o test.exe
But when I tried to execute via console it doesn't print anything.
And when I tried to execute by double clicking "test.exe" then it showed me this
Finally I found what causes this error.
There was a faulty app which is set in my system path variable containing a faulty libstdc++-6.dll which causes this error. I remove the path from th system path variable and now everything working fine.

geany: C++ Including libraries and headers

I'm very new in Ubuntu and programming C++ on Ubuntu using Geany.
The problem I have here is that:
the classes i want to iclude to my project will receive an error,
I type,
#include <vector>
the error given here is,
fatal error: vector: No such file or directory
also I cannot use namespace std,
typing using namespace std returns the following error,
error: unknown type name 'using'
Here is the code:
#include <stdio.h> //no problem here
#include "stdlib.h" //no problem here
#include <vector> //this is a problem (lets say it returns error 1)
using namespace std; //this is a problem (lets say it returns error 2)
int main(int argc, char **argv)
{
return 0;
}
This sounds like you are using the wrong compiler to compile your C++ code. For example, by invoking gcc test.cpp the C++ file is actually compiled as C and you receive errors such as the one you posted - there is no vector header in C and there is also no using keyword.
If you are using gcc, the correct way to invoke the compiler to compile C++ is via the g++ symlink, i.e. g++ test.cpp
If you are using clang, the executable is called clang++ instead.
Both compilers support the -x parameter to manually change the language to C++, although in that case you also have to specify that the compiler needs to link your files with the C++ standard library. For example: gcc -x c++ test.cpp -lstdc++

fatal error C1083 during build of program to test CStrings

I am trying to explore the CString Data type which is used extensively in my company's test program.
Here is the code:
#include <iostream>
#include <string>
#include <afx.h>
using namespace std;
int main()
{
CString cs("meow");
wcout << cs << endl;
return 0;
}
The above code compiles with 0 errors. However, when I try to build it, I get the following error.
fatal error C1083: Cannot open include file: 'atlstr': No such file or directory
I am using Visual C++ 6 Standard Edition for development.
Please note that my company's test program can compile and run well and I don't get the aforementioned error.
Is there a place where I can download the atlstr include file?

Emscripten: algorithm.h file not found

I am trying to compile the following code:
#include<stdio.h>
#include <algorithm.h>
int main() {
printf("hello, world!\n");
return 0;
}
But when I run emcc test.c -o test.html I get the following error:
fatal error: 'algorithm.h' file not found
When I remove the line that imports algorithm.h the code compiles perfectly.
Why is this happening? I was under the impression that algorithm.h was part of the standard library.
Edit:
I changed the name of the file from test.c to test.cpp, I updated the header names to <cstdio> and <algorithms>, and I also set -std=c++11 and it works now.
If this is C++ use
#include <cstdio>
in place of stdio.h and
#include <algorithm>
instead
In standard C++ there is no <algorithm.h> - there is only <algorithm>
Also in C++ the stdio header is both accessible from <cstdio> and <stdio.h> for compatibility.
Also since you are including algorithm the file extension should be .cc or .cpp and not .c or else emcc/gcc will treat it as a C source instead of a C++ one.

When I submit it online, they always says compile errors

That's my code:
It works good on my mac.
But I'm not sure is that the problem of Gcc version or not.
they said the sstream and string header are wrong.
1779655.134485/Main.c:8:19: fatal error: sstream: No such file or directory
compilation terminated.
here is the hint
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
You are trying to include C++ header file in your C program.
Gcc is C compiler.
You need to rename Main.c to Main.cpp and use g++ compiler...