-2

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.

Jarisleif
  • 113
  • 3
  • 10

3 Answers3

4

You have to declare and use functions like this:

#include <iostream>

int square(int x) {  // function declared and implemented
    return x*x;
}

int main(void) {

    for(int i=0; i<100; ++i) {
        std::cout<<i<<'\t'<<square(i)<<'\n';
    }

    return 0;
}

Or:

#include <iostream>

int square(int x);  // function prototype

int main(void) {

    for(int i=0; i<100; ++i) {
        std::cout<<i<<'\t'<<square(i)<<'\n';
    }

    return 0;
}

int square(int x) {   // function definition/implementation
    return x*x;
}

But for a simple piece of code like this it's not worth making a function - instead you can just do i*i inside the for loop to achieve the same result without needing to call a function. Alternatively, you could use pow(x,n) from <cmath> but again that's not really necessary here.

Also note the ++i in the for loop in my example, it's not important here but it's good to learn from the beginning that doing i++ creates a copy of the variable i then increments it - whereas doing ++i does not make a copy. This is irrelevant when i is just an integer (for example), but it becomes more important later on when it could be a different entity such as an object which may be more expensive to copy.

Additionally, it's best not to put using namespace std; as this creates namespace pollution - see here for details:

Why is "using namespace std" considered bad practice?

Community
  • 1
  • 1
sjrowlinson
  • 3,297
  • 1
  • 18
  • 35
  • Thank you, ArchbishopOfBanterbury, for your suggestions. Both worked. Initially, I did put i*i instead of function -- but that very exercise required the use of the **square()** function. I am especially grateful for the **++i vs i++** and **namespace pollution** tips. – Jarisleif Apr 08 '16 at 09:36
3

The compiler is right: neither C nor C++ provides a function named square in the standard library. Further, int square(i); is not a function declaration. It creates an int variable named square with an initial value of i.

What is the code supposed to do?

Pete Becker
  • 74,985
  • 8
  • 76
  • 165
  • It was supposed to output a table of numbers from 0 to 99 and their squares. Have just fixed that, thank you. – Jarisleif Apr 08 '16 at 10:27
1

There is no such function as square() in cmath. You need pow(base,exp). The following code should compile.

#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'<<pow(i, 2)<<'\n';
     }
 }
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
Prabhu
  • 3,434
  • 8
  • 40
  • 48
  • That works, but it's a lot of overhead for computing the square of an integer value. `int square(int x) { return x * x; }`. – Pete Becker Apr 08 '16 at 12:51