'pow' Was Not Declared In This Scope - c++

#include <iostream>
#include <string.h>
using namespace std;
int main()
{
int e=0;
int b=0;
cout<<"Enter Exponent";
cin>>e;
cout<<"Enter Base";
cin>>b;
pow(e, b);
cout<<"Power:"<<e;
return 0;
}
void pow(int e, int b)
{
int t=1;
while(b==t)
{
e=e*b;
t++;
}
}
Here is the error I received:
ulaga.cpp|29|error: 'pow' was not declared in this scope
Can any one explain why this error occurred?

The C++ compiler parses through your code file sequentially in order. i.e. line 1 then line 2 then line 3... and so on. So by the time the compiler comes to the function call statement pow(e, b); in your main() function, it hasn't yet reached the definition of the function void pow(int e, int b) below the main() function and therefore gives you the error. There are two ways to solve this.
1) Move the definition of void pow(int e, int b) (and any other function that you plan to call from main()) above the main() function itself. This way the compiler has already parsed and is aware of your function before it reaches the pow(e, b); line in your main().
2) The other way is to use a forward declaration. This means adding the line void pow(int e, int b); before the main() function. This tells the compiler that the function given by the forward declaration (in this case void pow(int e, int b)) is defined in this code file but may be called before the definition code of the function in the file. This is a better method as you may have multiple functions in your file calling one another in different order and it may not be easy to rearrange their definitions to appear before they are called in a file. Here's a good read on Forward Declaration
You may also want to pass parameters by reference to your function to get the correct result. i.e. use void pow(int& e, int& b). This will cause the values modified in your pow() function to actually be applied to integers e and b and not just to their copies which will be thrown away after pow() is done executing. This link about passing arguments by reference in functions is pretty good at explaining this.

You need to forward declare the pow function. Like -
....
void pow(int e, int b);
int main()
....
There are few things wrong here. For example e is passed by value. So, e in main is different from that of pow.
pow(e, b);
cout<<"Power:"<<e; // This just prints the value taken from console
// And not the calculated power.
Either make pow function to return a value (or) pass e by reference.

This can easily be fixed by just importing #include <cmath> in the beginning of your code.

You need to use forward declaration of your function pow. Or just move it's definition above the main function.

I was getting a similar error but I just added it.
#include<cmath>
and the "pow" not declared error got resolved.

Related

"Call of overloaded function is ambiguous" even with different argument order

I have a function fun() I wish to overload in the same scope. As per the rules of overloading, different order of arguments should allow for the overloading of the function as mentioned here.
The Code:
#include "iostream"
using namespace std;
void fun(int i, float j)
{
cout << "int,float";
}
void fun(float i, int j)
{
cout << "float,int";
}
int main()
{
fun(20,20);
}
Error:
error: call of overloaded ‘fun(int, int)’ is ambiguous
15 | fun(20,20);
Question:
Had there been only one function with argument fun(int, float), that would have been called as the best match, so why does it throw error in this case.
You give two integers, so the compiler have to convert one into a float, but which function shall be taken?
int main()
{
fun(20.0,20);
fun( 20, 20.0);
}
these calls makes the compiler happy, since you tell which function shall be taken.
The reason why you are facing this error is due to the data type of the arguments you have provided
In this case
fun(20,20);
which are both int, int.
A correct function call in this scenario would be,
fun(20,20.0);
or
fun(20.0,20);
(edit)
as mentioned by Ivan Vnucec
it would be better to explicitly call the function with arguments that are float instead of double
so call it in this way:
fun(20,20.0f);
or
fun(20.0f,20);

How to call a function from header file in c++

I'm new in C++
I was using java before, I'm trying to call a function in c++ from a hearder file.
Here is my code:
sum.h
int sum(int a, int b);
cur_time.h
#ifndef CUR_TIME_H
#define CUR_TIME_H
clock_t clock(void);
#endif /* CUR_TIME_H */
main.cpp
#include <iostream>
#include "sum.h"
#include "cur_time.h"
int main ()
{
int x;
int y;
x = sum(3,4);
std::cout << x;
y = clock(void);
y = std::cout << y;
return 0;
}
So in main.cpp I try to display the elapsed time of my system with this function: clock_t clock(void);
When I run the project, I have this error:
main.cpp:13:13: error: expected primary-expression before ‘void’
main.cpp:14:3: error: ‘__ostream_type’ was not declared in this scope
If I run the code without calling the second function it works for sum.
You don't have to (and mustn't) use void in parentheses on calling a function.
assigning std::cout << y to y is meaningless here and gcc gave me this error:
error: invalid user-defined conversion from 'std::basic_ostream' to 'int' [-fpermissive]
Therefore, main.cpp should be like this:
#include <iostream>
#include "sum.h"
#include "cur_time.h"
int main ()
{
int x;
int y;
x = sum(3,4);
std::cout << x;
y = clock(); // remove void
std::cout << y; // remove y =
return 0;
}
Have you defined your sum(int, int) function in Sum.h?
Simply doing int sum(int a, int b); is not good enough, you need to define that function.
int sum(int x, int y) {return x+y;}
Let's take that first error message as a clue. I encounter it a LOT, in various forms, when I write C++ code.
main.cpp:13:13: error: expected primary-expression before ‘void’
The phrase "expected primary-expression" almost always means that you have A) forgotten something, or B) typed something weird or unexpected.
For example, you will get the same kind of error if you were to try cout << "Hello, " < "world!" << endl; In that case, I put a < where I should have put a <<. (Actual example from my code two days ago.)
In your case, the error message is screaming about void, which means that void or whatever precedes it is out of place. In your line y = clock(void);, we know that the preceding token ( is correct...we're calling a function after all. That must mean that void is mistyped or out of place. Obviously it isn't mistyped. If you drop it, your code works.
What's up with that? Well, unlike some languages, C++ can accept an empty arg list, so long as the function wasn't expecting any arguments. y = clock(); will properly call the function.
[BONUS: If you're trying to call a constructor without any arguments, leave off the parenthesis altogether in the call. It's just one of those weird exceptions.]
So, what about that second error? In general, you should try to fix only the error at the top of the list (unless you know for certain what the other(s) are about). Many times, one error causes other weirder errors to happen as well. To see what I mean, try leaving off a semicolon and check out the errors you get as a result. Fix that error and recompile. If the second error shows up again, THEN you address it.
In regards to your comment to MikeCAT, this is a separate issue, but I'll address it the same anyway. Your clock() function returns clock_t, but you are assigning it to y, which is of type int. It appears that clock_t is based on int, so you should be able to get away with it. However, it may be a good idea to switch y to type clock_t as well, and see if that resolves it.
Beyond that, it may have to do with how you're using clock, though I'm not an expert on that class. If you continue to have problems, create a new question. On StackOverflow, you want to stick to ONE problem in ONE question. :)
(See my comment to your question for some additional tips on surviving and thriving around here.)

Using input argument as output

my question is:
I saw(in CUDA examples) that it is possible to use one of the input arguments of a function as output variable. Example: add two integers, c=a+b:
void function AddT(int a,int b,int c){
c=a+b;
}
But this will not work. The function will not alter the value of c in the main program. Who can I fix it and allow the function to change the value of c?
Pass the variable c by reference.
void function AddT(int a, int b, int& c)
{
c = a + b;
}
This will make it so that any changes to c that you make in the function will remain even after the function ends. My wording is pretty poor here; you can look here for more information:
Pass by Reference / Value in C++
What's the difference between passing by reference vs. passing by value?

C++ Defining Global Variable

Very simple question:
I was fiddling with basic C++ (being very new to programming) and I got into trouble while declaring a global variable to do some addition
#include <iostream>
int x,y;
int sum(int, int)
{
return x + y;
}
int main()
{
using namespace std;
cout << "The sum of 10 and 4 is: " << sum(10,4) << endl;
return 0;
}
Changing "int x,y;" to "int x,y = 0" has the same result: The sum equates to 0.
Could someone explain this odd behavior? Thanks!
Your function always returns the sum of global variables x and y, which are always 0. x and y are implicitly set to zero at the program startup. You never change their values, so they remain zero forever. The sum of two zeros is zero, no surprise here.
You pass 10 and 4 to your function, but the function itself completely ignores what is passed to it, i.e. it ignores its parameters (they are not even named). It always sums global x and y, which are always 0.
If you want your function to sum its arguments, you have to name the function parameters and use them
int sum(int a, int b)
{
return a + b;
}
And now you don't need any global variables at all. (main remains as is.)
Alternatively, if you so desire, you can get rid of the parameters completely and sum the global variables instead
int x,y;
int sum()
{
return x + y;
}
but in this case you will have to pass the values to sum through those global variables, not as function arguments
int main()
{
using namespace std;
x = 10;
y = 4;
cout << "The sum of 10 and 4 is: " << sum() << endl;
return 0;
}
This latter approach is here just for illustrative purposes. It is definitely not a good programming practice.
What you have in your code is a weird disconnected hybrid of these two approaches, which can't possibly work.
In order to fix the issue, the thing requires changing is the sum function.
int sum(int a, int b){
return a+b; //a,b here are referring to the inputs, while what you did was referring to the global variable..
}
Besides, try not to use global variables, usually you would end up with lots of troubles.
Another thing, I don't think your way of defining a function is correct. The inputs have to look like this instead:
int sum(int a, int b)
Unless you wanna declare the function first and provide the actual implementation later, you are not suppose to miss the name of the inputs!
when you are just globally declare the variables x,y ,they implicitly set to zero value.in your function definition,you are just giving the datantype of args, not the args names.so when you returning the sum of x,y ,it returns zero.and the value passed by the main function goes nowhere.
your program must look like this
#include<iostream>
int x,y;
int sum(x,y)
{
return x+y;
}
int main()
{
int v,a,b;
cout<<"values of a and b";
cin>>a>>b;
v=sum(a,b)
cout<<"their sum is"<<v;
}
when you explicitly define the value in second case
i.e int x,y=0;
you are just explicitly giving the value of value y to 0 while the x implicitly remains 0 and since you are not giving the args name,the ultimately result return biy the function is zero,
Seems that you only need x and y inside your add function, so make them local to the function. There is no reason to make them global. Follow the "least accessibility" idiom to prevent other parts of your program from mistakenedly modifying variables.
You might need a global variable supposed you want to define a well known parameter that every function needs to know and yet modifiable during run time. If you want it fixed, then a global constant would be more proper.
Hope that helps.

"invalid use of non static member function" What is this?

EDIT: thanks for all the speedy responses, I have a much better understanding of this concept now. Also, I'll try to make my error messages more clear next time.
EDIT: updated with my newest code. the error happens on line 18. Also, I'm beginning to wonder if my latest issue has to do with the original class itself?
I'm trying to teach myself classes and objects in C++. I did it once by just declaring a void function, outputting something on the screen, calling the object in main and everything worked fine.
Now, I wanted to expand upon this and make a simple addition thing. However, I get a couple errors on Code Blocks:
error: invalid use of non-static member function 'int Addition::add(int, int)'
error: no matching function for call to 'Addition::add()'
Here's my code:
#include <iostream>
using namespace std;
class Addition {
public:
int add (int x, int y) {
int sum;
sum=x+y;
return sum;
}
};
int main()
{
int num1;
int num2;
int ans=addobj.add(num1,num2);
Addition addobj;
addobj.add(num1,num2);
cout<<"Enter the first number you want to add"<<endl;
cin>>num1;
cout<<"Enter the second number you want to add"<<endl;
cin>>num2;
cout<<"The sum is "<<ans<<endl;
}
One of the most important things, a developer should learn to do is to read compiler's messages. It's clear enough:
error: no matching function for call to 'Addition::add()'
Your function in your class is
int add (int x, int y)
it takes 2 arguments and you pass none:
addobj.add();
You have 2 options:
create and initialize x and y inside your main and pass them as arguments
make add without parameters, create x and y inside add's body, as their values are taken from user input.
In this case, as the function's name is add, I'd chose the first option:
declare int x, y; inside your main
read the user input inside the main (the part, where you use cin and cout)
pass the x and y as arguments to add like this: addobj.add( x, y );
store the result (if needed), like this: int result = addobj.add( x, y );
You declared a method add(int, int) that takes two integers as arguments; you have to supply those arguments when you call it. It would be nice to print the returned value, as well:
Addition addobj;
std::cout << addobj.add(1, 2) << std::endl;
Your add function takes two arguments, yet you call it with none, so no matching function could be found. You must call the function as it was declared, i.e.,
addobj.add(1, 2);
Your function takes two arguments and yet you call it without providing them. You need to provide the two integer arguments that your function requires. To be useful you should store the result too. Something like this
int a = 1;
int b = 2;
int result = addjobs.add(a,b);