fstream does not function in Qt Creator - c++

I am new in Qt. I have my project coded in visual C++ 2010 express edition which works totally fine. Then, I want to use .cpp files that I already created in Qt console application (similar to my project in VC++2010). Compiling fails at "ofstream" and "ifstream" with this error:
"no matching function for call to 'std::basic_ofstream<char>::basic_ofstream(std::basic_strinstream<char>::_string_type)'"
"no matching function for call to 'std::basic_ifstream<char>::basic_ofstream(std::string&)'"
I have included fstream to the cpp file as:
#include <QCoreApplication>
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <sstream>
using namespace std;
and the piece of the code which fails is as follows:
stringstream s1; s1 << "Estimated_NN_ThrMWh.csv";
ofstream outF1(s1.str());
By the way, I am using "MinGW 4.9.1 32bit" as my compiler. What is the issue and how can I fix it? Thanks for your help.

You are passing a std::string to the std::ofstream constructor. This is a C++11 feature and to use this you need to pass -std=c++11 to GCC or Clang. MSVC automatically compiles for its hybrid not-quite-C++11-or-anything-else language that the compiler release compiles.
If you're using Qt 5's qmake, you can do just CONFIG+=c++11 and you're good to go. Otherwise you'll need something like
*gcc*:QMAKE_CXXFLAGS += -std=c++11
If you don't want to depend on C++11, don't use it, and just do:
std::string filename("Estimated_NN_ThrMWh.csv");
std::ofstream outF1(filename.c_str());
which will always work. Note I removed the stringstream because, at least in the short code you show, it's superfluous.

you should always write as following:
string s = s1.str();
ofstream outF1(s);
And, in my opinion, You code works under MSVC should be considered as a bug of MSVC (which allows non-const reference bind to a temporary object).

Related

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

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.

C++ <iostream.h> Error

I am at the absolutely newest level of new when it comes to C++. It may seem like a noob mistake, but I think I'm missing something with my first program, "Hello World!".
I'm running from Ubuntu (not sure if this is any different from working with Windows), and I'm using a book called Teach Yourself C++ in 21 Days.
The code I'm resembling looks exactly like this:
#include <iostream.h>
int main()
{
cout <<"Hello World!\n";
return 0;
}
I have this exactly in my text editor, but I keep getting greeted by the same error whenever I try to compile it!
first.cpp:2:22: fatal error: iostream.h: No such file or directory
compilation terminated.
I'm pretty distressed as this is literally the first step in my coding career! I'm not sure if ubuntu needs to be treated differently than Windows (which is what the book is using as reference).
Help!
There are two problems here:
You need to omit the .h suffix:
#include <iostream>
Also, cout is an unqualified name, and needs to be qualified with the std namespace since you are not using namespace std:
std::cout << "Hello World!\n";
There shouldn't be any iostream.h it's simply called iostream and should look like this:
#include <iostream>
int main()
{
std::cout <<"Hello World!\n";
return 0;
}
(also notice the std:: before the cout, since it means that it's from the standard namespace.)
You want just iostream
#include <iostream>
I suspect the book is very old. Names are qualified in the std namespace, so you may want to add
using namespace std;
For now at least.
use
#include <iostream>
in general STL header file don't have a .h
you need to compile using g++ compiler, not by gcc
g++ hello.cpp

C++ cout gives undeclared identifier

So, I have this question. Why does cout throws
error C2065: 'cout' : undeclared identifier
I am using Visual Studio 2012 as an IDE and I am writing a school project. I have everything done except an example file. So I am trying to write something on the screen like this:
#include "iostream"
#include "stdafx.h"
using namespace std;
int main()
{
cout<<"example";
return 0;
}
So the problem is with cout... printf works fine, but I want to use cout.
EDIT:
I've changed "" to <> but it is not helping. Also I am using this code only for example... This is not the whole project.
stdafx.h shall be the first include directive in your source file.
Switch files and convert the second include to <>, as other suggested.
#include "stdafx.h"
#include <iostream>
See this post for more information.
First of all:
#include <iostream>
instead of #include "iostream"
Secondly, it is generally considered bad practice to write using namespace std;, even though most courses start with that. It is better to only use what you actually need, in your case:
using std::cout;
#include "iostream"
should be
#include <iostream>
Quoting from this post:difference-between-iostream-and-iostream-quotes-in-include
By courtesy of #Jerry Coffin's answer:
When you use < >, the compiler only looks in the system-designated directory/directories (e.g., whatever you've set in the include environment variable) for the header.
When you use " ", the compiler looks in the local directory first, and if that fails, re-searches just like you'd used < >. Technically, (i.e., according to the standard) that doesn't have to be the "local" directory, but that's how it works in essentially every compiler of which I'm aware).
EDIT:
However, the root cause is that stdafx.h is a precompiled header. Visual C++ will not compile anything before the #include "stdafx.h" in the source file, unless the compile option /Yu'stdafx.h' is unchecked (by default); it assumes all code in the source up to and including that line is already compiled. However, it is still better to use <> with iostream not to confuse reader of the code.
If you use #include <iostream> with the <> instead of "" then it should work. Right now, the compiler doesn't know where to find the iostream library.
Also, you might want to change cout<<"example"; to cout<<"example"<<endl; for a new line so that it formats correctly.
Came across this issue while trying to build a Dynamic Linked Library. Make sure that instead of the #include stdafx.h you specify the following include on the first line of your .cpp file:
#include "pch.h"
This should also be the case for VS2017 or earlier.
This error also occurred in the Visual Studio 2017 IDE. Moving stdafx.h to the top solved the error.
For more on stdafx.h, see What's the use for "stdafx.h" in Visual Studio?

program does not compile with newer version of g++

I have the following source code. Which compiles fine in visual studios and g++ 3.4.6; but not with g++ 4.4.3 (on a newer ubuntu machine). The newer compiler requires that I explicitly include to use atoi. I am just trying to figure out what might have changed to cause this behavior. Is it sstream header file previously included cstdlib and no longer does so. Or is it the compiler behavior that has changed.
#include <sstream>
int main()
{
char str1[]="123";
int i = atoi(str1);
printf ("value = %d",i);
return 0;
}
You also need to include <cstdio> for printf().
Technically, if you include the headers of the form <cname> instead of <name.h>, you also need to qualify the names from the standard library using std::. A lot of standard library implementations are relaxed when it comes to this, though, and also put the names into the global namespace.
It's implementation-dependent which headers are included by which other headers, so you should always be sure to include all the headers that you need and not assume that they will be included automatically.
I'm using GCC 4.4.5 on Debian, and the headers have changed so you will not bring in the headers necessary. You need to #include <cstdlib> and #include <cstdio> to get atoi and printf, as the compiler complained about both being missing.
#include <sstream>
#include <cstdio>
#include <cstdlib>
int main()
{
char str1[]="123";
int i = std::atoi(str1);
std::printf ("value = %d",i);
return 0;
}
Well yes. That is common. You should always include ALL headers that you are directly using and not depend on the fact that those headers are already included.
Compiler behavior is what would have changed... the <sstream> doesn't use atoi.
Arguably you should have always done #include <cstdlib>, and you'd gotten lucky with your previous compilers.
As James McNeillis points out, you should also #include <cstdio> in order to use the printf function.