I have a code including

#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

And it works with MinGW under XP.
I want to compile the same code under Suse linux,
but compiler said that for example:
atof(), error() functions are not defined.

The problem solved if I include

#include <stdio.h>
#include <stdlib.h>

also, but I don't understand!
The packages are different under XP and linux?
I want a platform independent code.

Dani AI

Generated

Short answer: this is a portability/headers issue, not a compiler bug. As pointed out, some implementations bring in other headers implicitly; your MinGW build happened to get declarations transitively, while the SUSE headers did not. Relying on transitive includes is fragile — include the header that actually declares the name you use and prefer the C++ variants and namespace-qualified names for portability.

For numeric conversions prefer modern, safer APIs instead of raw atof. std::atof comes from the C library, but it gives no error info; use std::strtod (gives an end pointer) or std::stod (C++11, throws exceptions) so you can detect failures:

double v = std::stod(s); // C++11+, throws invalid_argument or out_of_range
char *end;
double v = std::strtod(s.c_str(), &end);
if (end == s.c_str()) {
    // conversion failed
}

About error(): it is not part of standard C or C++. Some platforms (glibc) provide a nonstandard error() in <error.h>. If your code used error() without including its header, it might have compiled on one platform due to transitive includes but fail elsewhere. Replace nonstandard calls with portable alternatives: perror or std::cerr for simple messages, or use std::system_error / std::error_code for robust C++ error handling.

Practical checklist: always include the header that defines the symbol; prefer C++ headers (<cstdlib>, <cstdio>, etc.) and std:: qualifiers; compile with warnings (-Wall -Wextra -pedantic) and an explicit standard (-std=c++11 or later); and prefer std::from_chars/std::stod/std::strtod over atof for reliable parsing. Your quick fix worked, — but applying the above will make the code truly portable.

The packages are different under XP and linux?

No. Your code under Windows was benefiting from internal includes. That is, standard headers themselves include other standard headers, which means somewhere in <iostream>, for example, <cstdlib> was included. atof is declared in <cstdlib>, so your calls magically worked.

It's best to include all of the headers you need, even if the code still compiles without them. Of course, this also means you should know which header to include for all of the standard names you use. ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.