Incurring the "‘square’ was not declared in this scope" error while trying to compile a simple program with the "square()" function.
I am doing a "Try this" drill (the "Functions" section) from Stroustrup's Programming: Principles and Practice Using C++, and the code won't compile.
Here's the code:
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
int main()
{
for(int i=0; i<100; i++){
cout<<i<<'\t'<<square(i)<<'\n';
}
}
And this is what my compile returns:
function.cpp: In function ‘int main()’:
function.cpp:10:26: error: ‘square’ was not declared in this scope
cout<<i<<'\t'<<square(i)<<'\n';
I made another attempt and placed a function declaration inside of the for-loop:
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
int main()
{
for(int i=0; i<100; i++){
int square(i);
cout<<i<<'\t'<<square(i)<<'\n';
}
}
And the reply was:
function.cpp: In function ‘int main()’:
function.cpp:11:26: error: ‘square’ cannot be used as a function
cout<<i<<'\t'<<square(i)<<'\n';
Besides, I was placing the "square()" function declaration outside the loop inside the body of the "main" and before the body of the "main": the reply was identical to the response to my attempt #2.
The compiler and the OS are:
gcc version 5.2.1 20151028 (Ubuntu 5.2.1-23ubuntu1~15.10)
I am a novice, and most probably mistake lies in something very simple -- however I think I have tried all options conceivable. As for the "std_lib_facilities.h" file, it doesn't work and gives a "deprecated header" reply.
I would be hugely grateful for any help and advice.