João João - 2 months ago 26
C++ Question

error function not declared in scope

I'm trying to log a few values to a text file, but everytime I do it, it gives me this error:

[Error] 'save' was not declared in this scope

Here's the code:

#include "iostream"
#include "fstream"

using namespace std;
double n1, n2;

int main()
{

n1 = 1;
n2 = 2;
save(n1, n2)

}
int save(double a, double b)
{
ofstream log;
log.open("test.txt");
log << 1 << 2 << "\n";
log.close();
}


I'm new to C++ and programming in general, please explain in an easy way.

Thanks.

Answer

Code in main needs to know that such a function exists. It needs either a declaration or a definition above it:

int save(double a, double b);

int main() //...

By the way: you should eschew using global variables. And you're not using the parameters in your save function.