| by Arround The Web | No comments

Error: Undefined Reference to Pow in C++

When working in programming languages, they must contain libraries, functions, syntax, etc. So, there’s a potential for error in our programs. There are various errors of different kinds in C++ programming. Here, we will discuss the “undefined reference to pow” error in C++ programming. Sometimes, it appears as “pow was not declared in this scope” and both have the same meaning. This error occurs when we don’t add the header file in our C++ code or we may not compile the code correctly. We will also explore the header files that aid in removing that error from our C++ code.

Solving the Undefined Reference to Pow in C++

When tackling this error message, we simply add the header file in our code in which the “pow()” function is declared. This removes the error from our code. The header files that are utilized for the “pow()” function are:

  • #include <cmath> header file
  • #include <bits/stdc++.h> header file
  • #include <math.h> header file

Here are some codes that will show how this error occurs and how to tackle this error in C++ programming.

Example 1:

In our first example, we add the “#include <iostream>” header file which aids in performing the input and output tasks. Then, we have to write “using namespace std” after this header. This aids in preventing the confusion that could arise from two IDs that share the same name.

After this, we call the “int main()” function here which is utilized to define the start of our C++ code. Now, in this function, we utilize the “cout<<” statement which aids in printing the given information on the display screen. The “<< endl” statement is utilized to shift our pointer to the new line.

After this, the “pow()” function is used in which we have to pass two parameters: base and exponent. Here, we add “10, 4,” in which “10” is the base number and “4” is the exponent. At the end of this code, we insert one more statement which is "return 0”.

Code 1:

#include <iostream>
using namespace std;
int main() {
  cout << "The power function is utilized here ";
  cout << endl <<endl ;
  cout << pow(10, 4);
  return 0;
}

Output:
After completing the code, we have to compile and run it. When we compile the previous code, the error message that states “pow was not declared in this scope” appears. This error appears because we haven’t added the header file in which this “pow()” function is declared. To solve this error, we have to insert any of the header files that we mentioned earlier.

Now, we need to solve this error to get the result. For this, we made a little bit of changes in the code. In the same code, we add the “#include ” header file after the “” header file. This helps remove the error of the previous code because the “pow()” function is declared in this header file.

Updated Code 1:

#include <iostream>
#include <cmath>
using namespace std;
int main() {
  cout << "The power function is utilized here ";
  cout << endl <<endl ;
  cout << pow(10, 4);
  return 0;
}

Output:
This updated code is now compiled and is running successfully, and we get the output of the code. The error of the previous code is now removed and the following result appears after compilation:

Example 2:

Here, we have another example in C++. After adding the “<iostream>” and the “std”, we now call the “main()” function. Inside the main(), we simply print a line with the help of a “cout” statement. Then, we declare three variables of integer data type named “num, exp, and result”. After this, we initialize “num” with “25” and “exp” with “2”. The “result” variable is initialized with the “pow()” function in which “num” and “exp” are passed as the two parameters of this function. After this, we display that power result using “cout”.

Code 2:

#include <iostream>
using namespace std;
int main()
{
    cout << "We are finding the power of a number here in C++ Programming. " <<endl<<endl;
    int num, exp, result;
    num=25;
    exp=2;
    result= pow(num, exp);
    cout<< "The power of the number is = " << result ;
    cout<<endl;
    return 0;
}

Output:
Now, this new code shows the same error message as the previous example. The reason is that it is also the same because we didn’t insert the header file by which we can get this “pow()” function.

Now, we add the header file to our code which is the “cmath” header file. This header file is used in C++ programming to get our code’s “pow()” function. After adding the “#include ” header file, we get the required output of our code.

Example 3:

Now, we have one more example. Here, after the “main()” function, we declare and initialize the “base_num” of “long double” data type with “4.5”. Then, we also declare and initialize the “expo_num” of “integer” data type with “-2”. We also declare one more variable of the name “result_1” which is of “long double” datatype.

Now, we use the “pow()” function to initialize the “result_1” variable and pass “base_num” and “expo_num” to this function. After this, we print the result using the “cout” command. Now, we declare and initialize two more variables, “f_baseNum” and “f_expoNum”, with the “float” data type. We initialize them with “-9.2” and “5”, respectively. Then, we declare “result_2” as a “double” data type. We now initialize the “result_22” variable using the “pow()” method, passing in “f_baseNum” and “f_expoNum”. Next, we use the “cout” command to print the outcome.

Code 3:

#include <iostream>
using namespace std;
int main () {
  long double base_num = 4.5;
  int expo_num = -2;
  long double  result_1;
  result_1 = pow(base_num, expo_num);
  cout << "The base_num here is in long double and expo_num is in integer type " <<endl<<endl;
  cout << base_num << " ^ " << expo_num << " = " << result_1 << endl<<endl;
  float f_baseNum = -9.2, f_expoNum = 5;
  double result_2;
  result_2 = pow(f_baseNum, f_expoNum);
  cout << "The f_baseNum here is in float and f_expoNum is in double type " <<endl<<endl;
  cout << f_baseNum << " ^ " << f_expoNum << " = " << result_2;
  return 0;
}

Output:
After compiling this code, we get an error message that says that the power function was not declared in this scope. It means that we missed the header file in which this function is declared.

The error is now resolved using the “<math.h>” header file. This time, we use the “#include <math.h>” header file because this header file also aids in resolving that error.

After placing this, the code is successfully complied and we get the output of our code which is also shown here.

We may also get the output using another header file, the “<bits/stdc++.h>” header file. We make use of all the standard libraries in our programming with the aid of this header file.

After adding the “<bits/stdc++.h>”, we get the required output because “bits/stdc++.h>” contains every standard library. So, the error is now removed after adding this library to our code, and the output result is shown here.

Conclusion

In this article, the “undefined reference to pow” or “pow was not declared in this scope” error in C++ programming is also discussed in detail. We also explored the ways or header files that aid in removing the error for successful code compilation. We demonstrated the examples in which we showed the error message and the result of the code after removing that error message.

Share Button

Source: linuxhint.com

Leave a Reply