How to solve this error of function pointer? - c++

In a program a pointer is declared to function (p) which initialized to function (add)
i tried to read all the concepts related to function pointer.
But i couldn't solve it
Please help me to solve this program with out any errors.
#include <stdio.h>
#include <iostream>
using namespace std;
int add(int n1, int n2)
{
return n1 + n2;
}
int *functocall(int,int);
int caller(int n1, int n2, int(*functocall)(int, int))
{
return (*functocall)(n1, n2);
}
int main()
{
int a, b, c;
cin >> b >> c;
int (*p)(int,int)=&add;
a=caller(b,c,(*functocall)(b,c));
printf("%d",a);
return 0;
}
If the input is 20 70
Output must be 90

(*functocall)(b,c) doesn't do what you expect, you're trying to call on the functocall. (Note that functocall is declared as a function, which takes two ints and returns a int*.)
You should pass the function pointer itself to caller, e.g.
a = caller(b, c, p);
or
a = caller(b, c, &add);
LIVE

Way too complicated. it's often the case when you don't understand something you make it more complicated then it needs to be
#include <stdio.h>
#include <iostream>
using namespace std;
int add(int n1, int n2)
{
return n1 + n2;
}
int caller(int n1, int n2, int(*functocall)(int, int))
{
return (*functocall)(n1, n2);
}
int main()
{
int a, b, c;
cin >> b >> c;
a = caller(b,c,add);
printf("%d",a);
return 0;
}
Not too sure if you really need caller. Maybe you added than when you were trying to get it to work, but maybe not. An even simpler aternative is
#include <stdio.h>
#include <iostream>
using namespace std;
int add(int n1, int n2)
{
return n1 + n2;
}
int main()
{
int a, b, c;
cin >> b >> c;
int (*functocall)(int, int) = add;
a = functocall(b,c);
printf("%d",a);
return 0;
}

Related

Getting an error "invalid use of non-static data member 'stu::n' "

#include <bits/stdc++.h>
using namespace std;
struct stu {
int n;
stu(int _n = 0):n(_n) { }
int add(int a, int b = n-1) {
return a + b;
}
};
int main() {
stu obj = stu(5);
cout << obj.add(10) << endl;
}
The compiler shows the message " invalid use of
non-static data member 'stu::n' ".
What is wrong with this code. Any help would be great.
Thanks.
You can't use default arguments this way. Consider writing two separate functions:
struct stu {
int n;
int add(int a, int b) { return a + b; }
int add(int a) { return a + n - 1; }
}

__gcd function in visual studio version 1.38.0

I'm trying to find out the GCD in C++. I'm using __gcd code as mentioned on this website : https://www.geeksforgeeks.org/stdgcd-c-inbuilt-function-finding-gcd/
Appended is my code. Can someone please guide as to what is wrong
#include <iostream>
#include <string>
#include <numeric>
#include <algorithm>
using namespace std;
class Rational {
private:
int num;
int denom;
public:
Rational(int a, int b){
num = a;
denom = b;
}
int add(){
return num + denom;
}
int sub(){
return num - denom;
}
int mul(){
return num * denom;
}
void gcd(){
cout <<__gcd(num,denom);
}
int simplify(){
int gcd1 = gcd(num,denom);
return (num/gcd1,denom/gcd1);
}
};
int main(){
Rational r(2,6);
cout<<r.add()<<endl;
cout<<r.sub()<<endl;
cout<<r.mul()<<endl;
cout<<r.gcd()<<endl;

Can't return result from int function

I am making a class inherited program in c++. Its function is to determane whenever a triangle is right and to calculate its area.
Here is the program:
#include <iostream>
using namespace std;
class Puncte
{
public:
int x1,y1;
int x2,y2;
int x3,y3;
Puncte(int a, int b, int c, int d, int e, int f):x1(a),y1(b),x2(c),y2(d),x3(e),y3(f){}
void AfisareP()
{
cout<<x1<<y1<<x2<<y2<<x3<<y3<<endl;
}
};
class Latura
{
protected:
int l1, l2, l3;
public:
void LatimeaL(int a, int b, int c)
{
l1 = a;
l2 = b;
l3 = c;
}
};
class Triunghi: public Latura
{
public:
int AriaTrDr()
{
return l1*l3/2;
}
};
int main()
{
Puncte p(2,2,2,2,2,2);
Latura l;
l.LatimeaL(1,2,4);
Triunghi t;
if (p.x1 == p.x3 && p.y1 == p.y2)
{
cout << "Triunghi dreptunghic!" << endl;
cout << t.AriaTrDr() << endl;
}
return (0);
}
Everthing is working fine but it doesnt show the correct result but the adress and i dont know how to fix it.
This is the result.
Triunghi dreptunghic!
513242112
You are calling l.LatimeaL(1,2,4); on the object l.
The object t is created with Triunghi t;. It is not initialized by calling the Latimeal function. Therefore you are getting an undefined value.
You need to cal the t.Latimeal(1,2,4) function to initialize it, after creating the object.

mod function not working why?

#include<iostream>
using namespace std;
int c;
int fun_div();
int fun_div(int a,int b){
if(a%b==0){
c=1;
cout<<"Solution Available :\t"<<c;
} else
{
c=0;
}
return c;
}
int main(){
int c;
int e,d;
cout<<"enter two values : \n";
cin>>e>>d;
cout<<endl;
}
error in finding the mod of two numbers and not compiling the program :
error in finding the mod of two numbers and not compiling the program
It compiles for me
#include<iostream>
using namespace std;
int c;
int fun_div();
int fun_div(int a,int b)
{
if(a%b==0){
c=1;
cout<<"Solution Available :\t"<<c;
} else {
c=0;
}
return c;
}
int main(){
int c;
int e,d;
cout<<"enter two values : \n";
cin>>e>>d;
fun_div(e,d);
cout<<endl;
}
You should put the error message when asking about compilation errors. However I copied your code exactly and it compiles.
The other thing is that you don't call your function so I added that as well.
And as a side note, you could just do
int fun_div(int a, int b)
{
return (a%b == 0);
}
because (a%b == 0) will evaluate to 1 if a is a multiple of b and 0 otherwise.
#include<iostream>
using namespace std;
int c;
int fun_div();
int fun_div(int a,int b)
{
if(a%b==0){
c=1;
cout<<"Solution Available :\t"<<c;
}
else
{ c=0; }
return c;
}
int main()
{
int e,d;
cout<<"enter two values : \n";
cin>>e>>d;
c=fun_div(e,d);
cout<<endl;
}
Try this. I think this is what you expected.
Please explain your question to get a more specific answer.
I have added a function call to the function fun_div.
you should probably add one more check for which is greater.. the greater check will ensure that there is a proper remainder available
Well the main problem in your code is that you are not calling the function which you defined, that is why you are not getting the desired result, And there are some better practices for writing code which you should follow to avoid errors in future.
Don't use global variable and if you are returning the result from function then show on screen from main function.
Recommended Code is given below, i have changed the function so it will only check that 'a' is divisible by 'b' or not and return the value to main so it will show the result on screen.
#include<iostream>
using namespace std;
int fun_div(int a, int b)
{
return (a%b == 0);
}
int main() {
int e, d;
cout << "enter two values : ";
cin >> e >> d;
if (fun_div(e, d))
{
cout << "Solution Exists.";
}
else
{
cout << "No Solution Exists.";
}
cout << endl;
return 0;
}

Why is my vector not adding the struct?

I'm having problems with the last part of my project on this algorithm. I'm having a problem with the .size() of my vector. It doesn't give me anything. Everytime I try to debug it, it's 0. I don't know if that means that my function is not working properly.
I'm new to programing and I need some help.
Edit1: I've added the new code that is giving me an exception.
#include "stdafx.h"
#include <iostream>
#include <conio.h>
#include <vector>
using namespace System;
using namespace std;
typedef struct Linea {
int a;
int q;
int b;
int r;
} SLinea;
int algoritmoeuclides(int a,int b)
{
if (a%b==0)
return b;
return algoritmoeuclides(b,a%b);
}
std::pair<int, int>division(int dividendo,int divisor)
{
int resultado=0;
int residuo=0;
residuo=dividendo%divisor;
resultado=dividendo/divisor;
std::pair<int, int>retorno;
retorno.first=resultado;
retorno.second=residuo;
return retorno;
}
std::pair<int, int>EuclidesExtendido(int a,int b,vector<SLinea*> aux)
{
std::pair<int, int> tsqt;
int q,r,s,t;
SLinea *var;
if (b == 0)
return std::pair<int, int> (1, 0);
else
{
std::pair<int, int>qr = division(a, b);
q=qr.first;
r=qr.second;
std::pair<int, int>st = EuclidesExtendido(b, r,aux);
s=st.first;
t=st.second;
}
tsqt.first=t;
tsqt.second=s-q*t;
var->a=a;
var->b=b;
var->q=q;
var->r=r;
aux.push_back(var);
return tsqt;
}
int main()
{
int a=23;
int b=120;
int s,t;
vector<SLinea*> Ecuacion;
std::pair<int, int>st=EuclidesExtendido(a,b,Ecuacion);
s=st.first;
t=st.second;
int resultado=algoritmoeuclides(a,b);
int n=Ecuacion.size();
for (int i=0;i<n;i++)
{
printf("%d=%dx%d+%d\n",Ecuacion[i]->a,Ecuacion[i]->b,Ecuacion[i]->q,Ecuacion[i]->r);
}
printf("MCD= %d\n ",resultado);
printf("s=%d \nt=%d",s,t);
system("pause");
}
#include "stdafx.h"
#include <iostream>
#include <conio.h>
#include <vector>
using namespace System;
using namespace std;
typedef struct Linea {
int a;
int q;
int b;
int r;
} SLinea;
int algoritmoeuclides(int a,int b)
{
if (a%b==0)
return b;
return algoritmoeuclides(b,a%b);
}
std::pair<int, int>division(int dividendo,int divisor)
{
int resultado=0;
int residuo=0;
residuo=dividendo%divisor;
resultado=dividendo/divisor;
std::pair<int, int>retorno;
retorno.first=resultado;
retorno.second=residuo;
return retorno;
}
std::pair<int, int>EuclidesExtendido(int a,int b,vector<SLinea> aux)
{
std::pair<int, int> tsqt;
int q,r,s,t;
SLinea var;
if (b == 0)
return std::pair<int, int> (1, 0);
else
{
std::pair<int, int>qr = division(a, b);
q=qr.first;
r=qr.second;
std::pair<int, int>st = EuclidesExtendido(b, r,aux);
s=st.first;
t=st.second;
}
tsqt.first=t;
tsqt.second=s-q*t;
var.a=a;
var.b=b;
var.q=q;
var.r=r;
aux.push_back(var);
return tsqt;
}
int main()
{
int a=23;
int b=120;
int s,t;
vector<SLinea> Ecuacion;
std::pair<int, int>st=EuclidesExtendido(a,b,Ecuacion);
s=st.first;
t=st.second;
int resultado=algoritmoeuclides(a,b);
int n=Ecuacion.size();
for (int i=0;i<n;i++)
{
printf("%d=%dx%d+%d\n",Ecuacion[i].a,Ecuacion[i].b,Ecuacion[i].q,Ecuacion[i].r);
}
printf("MCD= %d\n ",resultado);
printf("s=%d \nt=%d",s,t);
system("pause");
}
std::pair<int, int> EuclidesExtendido(int a,int b,vector<SLinea> aux)
That function takes the parameter aux by value, which means that it gets a copy of whatever its used as an argument. I'll let you take it from there...