File Handling C++ with xcode. simple ofstream out not working - c++

I wanted to do simple file handling test program in Xcode. But it gives me an error.
#include <iostream>
#include <fstream.h>
using namespace std;
int main() {
ofstream out("test");
out<<"pen"<<"\t";
out<<"25";
}
Error:
Implicit instantiation of undefined template std::basic_ofstream<char>

The first error means that you have to write fstream instead of fstream.h . The second error would be caused by the first one. Look at my g++'s output when I try to compile your example:
example.cpp:2:21: fatal error: fstream.h: No such file or directory
compilation terminated.
In this link, you will find ofstream's class specification. I think it can be helpful for you:
http://www.cplusplus.com/reference/fstream/ofstream/

Related

C++ programming not compiling <iostream>

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"

There are unresolved includes inside <iostream>

I just tried to compile my C++ code and an error appears when I try to do so.
The error appears on line 9
Here are the versions of the gcc and g++ and such
Any help would be appreciated.
Edit:
I am also including Movie.h:
And also Movie.cpp:
https://puu.sh/vb53G/9e9abd1832.png (I was not able to include more than 3 images due to restrictions)
Firstly, in your Movie.h file, you have not included the string header file correctly. It should be:
#include <string> // without the .h extension
error: 'string' does not name a type
Secondly, you have forgotten to add the closing parenthesis of the constructor function of class "Movie". I am assuming that you have added this now, after the edit
As for the marking done by your compiler, you may find the following StackOverflow post helpful:
StackOverflow Post: Unresolved inclusion iostream.
The link is for the Eclipse IDE, but you can find a similar solution for your own IDE (I cannot tell which one you have).
The line under the #include is just a warning (I'm not sure why).
However, the errors are from the "Movie" class:
1. add "using namespace std" on the top of this class.
2. close the parenthesis on the constructor of 'Movie'.
The error messages are fairly clear:
'string' does not name a type
That is, the compiler is unaware of the type string because either:
you have not #include <string> in Movie.h
or you have, but have not brought it into your namespace with a using namespace std;
although why not just refer to it as std::string?
You are missing
#include <string>

error: no type named 'InterfaceProvider' in namespace 'content::shell'; did you mean '::shell::InterfaceProvider'?

I am getting the following compile time error when i am including the render_tree_node.h
in my example.cc
included from ../../content/shell/browser/my_example.cc:17:
In file included from ../../content/browser/frame_host/frame_tree_node.h:17:
In file included from
../../content/browser/renderer_host/render_process_host_impl.h:165:3: error: no type named 'InterfaceProvider' in namespace 'content::shell'; did you mean '::shell::InterfaceProvider' ?
when I compile this withuot adding my_example, it does compile the code.
I have tried adding the Dependency in my BUIDL.gn of my_example.
can anyone help ?
render_process_host_impl.h looks like this :->
namespace shell {
class InterfaceRegistry;
}
namespace content {
virtual shell::InterfaceRegistry* GetInterfaceRegistry() = 0;
}
mmExample.cc
#include "/render_tree_node.h"
my Question is when i compile myExample.cc the error mentioned above appears else it does not, there are other files are also there which are using this file and this file is compiling i have rechecked even without adding my files also.
what could be the problem ??

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

g++ compilier how to include boost::regex

I've installed boost, so i'm trying to run compiler like this:g++ -LC:\MinGW\boost_1_55_0\stage\lib -lboost_regex-mgw49-mt-1_55 test.cpp
My programm test.cpp is not very complicated:
#include <iostream>
int main()
{
std::cout << "Hello, world!" << std::endl;
boost::regex rex("[test]");
}
I'm of course failed: error: 'boost' has not been declared. I can't understand what I must write in my test.cpp file next to #include <iostream>? #include <boost> doesn't work, i'm getting error fatal error: boost: No such file or directory.
#include <boost/regex.hpp> also yield this error, maybe #include <boost_regex-mgw49-mt-1_55>? But it still doesn't work.
You need to include regex.hpp in your main. You hint that you tried to, but it apparently didn't work.
Make sure regex.hpp is where you tell the compiler it is relative to one of your search directories. It's not good enough to tell the compiler to include boost/regex.hpp, the file has to actually exist where the compiler is looking for it to be able to find it.
You will need to copy that file from wherever you downloaded it to somewhere the compiler will know to look for it. Once you do that, it should find it and that error should go away.
Also note that the same applies to the object file you're trying to link in. If the linker can't find that file, you'll just get a linker error once you clear up this compiler error.