geany: C++ Including libraries and headers - c++

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++

Related

C++11 function iota() not supported by nvcc?

I tried to compile this code:-
#include <vector>
using namespace std;
int main() {
vector<int> v(5);
iota(v.begin(), v.end(), 0);
}
And I compiled it with this command:-
D:\workspace\test>nvcc main.cpp --std=c++11
(Because without specifying the std I was getting the "identifier iota() not found" error)
And I get this error:-
nvcc warning : The -std=c++11 flag is not supported with the configured host compiler. Flag will be ignored.
main.cpp
main.cpp(7): error C3861: 'iota': identifier not found
How do I specify the C++ standard I want nvcc to use?
Also, compiling host code separately with g++ and device code with nvcc and then linking the objects with nvcc doesn't work. I get this.
I think you need to add #include <numeric>.
enter image description here
There is no need. By default the command line tool nvcc uses Microsoft's cl.exe. And if your cl.exe is updated, the std option is not available. cl.exe automatically supports all the latest C++ standard's features.
However, in cl.exe some of the functions like iota() aren't defined in the std namespace. Instead, iota() is defined in the numeric.h header file. So to run that code you would need to include the said header file. The final code should look like this:-
#include <vector>
#include <numeric.h>
using namespace std;
int main() {
vector<int> v(5);
iota(v.begin(), v.end(), 0);
}
The code can be compiled by the command:-
nvcc main.cpp

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.

Why g++ compiler is not able to find unique_ptr?

I am trying to compile a small C++ code which invloves unique_ptr as given below.
#include <iostream>
#include <memory>
using namespace std;
int main()
{
unique_ptr<int> p1(new int);
}
when I tried to compile the code using g++, it is throwing up 'unique_ptr' was not declared in this scope. I was trying to compile on Linux box. even I tried with '-std=c++11' option. It was saying 'unrecognized command line option -std=c++11'. Can any one please let me know how to fix this?
You need to include it, it comes out of the <memory> library
#include <memory>
According to the GCC 4.4 release notes, unique_ptr was not in GCC's standard C++ library before 4.4.
So you might want to check your GCC version first, using g++ --version like #40two said.

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...

Getting fatal error for iostream

I wanted to write a simple clang plug-in. So I just executed a "PrintFunctionNames" plug-in provided in llvm-clang. But when i tried to execute a command :
" clang -cc1 -load ../../Debug+Asserts/lib/libPrintFunctionNames.so
-plugin print-fns some-input-file.c "
it gives me 1 fatal error :
fatal error: 'iostream.h' file not found
#include<iostream.h>
^
1 error generated.
I also tried using -I option providing a path for include directory of 'iostream' but it's still gives me the same error.
I tried it like:
'clang++ -I//usr/include/c++/4.6 -cc1 -load
../../../../Release+Asserts/lib/libPrintFunctionNames.so -plugin
print-fns ak.cpp '
So how do I make this work?
Don't add .h at the end.
#include <iostream>
That should fix it.
iostream belongs to C++ not C. So you should include it as
#include <iostream>
Additionally since you are programming in C++ you should name your source file ending with .cpp not .c to make it clear to the compiler and everyone else, that you want to use C++. Also you might need to invoke clang++ in your first compiler call (but I am not sure about that in context of plugins)
After the C++ language was standardized by the ISO, the header file named iostream.h was renamed to iostream. Change your program to use #include <iostream> instead and it should compile.
You will also need to add using namespace std; statement after each include (or prefix each reference to an iostream function/object with std::).
You can start by using this
#include <iostream>
using namespace std;
Once you are more comfortable with namespaces, you can remove the using statement & instead either use std::cout, std::cin etc or have a
using std::cout;
using std::cin;
etc.
I meet the same question,
template.cpp
g++ template.cpp
compare<int>com1(3,7);
List item
compare<double>com2(12.34,56.78);
compare<char>com3('a','x');
cout<<",the max value:"<<com1.max()<<endl;
cout<<",the max value:"<<com2.max()<<endl;
cout<<",the max value:"<<com3.max()<<endl;
return 0;
the question is up code segment composing not OK, use the shift + table typing next time.