#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; }
}
I have been trying to code a program that can solve for c using the Law Of Cosines. The program runs correctly, but the answer I get is ridiculously big, noted by how it was in scientific notation.
Here is my code:
#include <iostream>
#include <cmath>
using namespace std;
class TrigMath
{
private:
double a;
double b;
double y;
public:
double LawOfCos()
{
return sqrt(pow(a,2) + pow(b,2) - 2*a*b*cos(y));
}
void seta(double A)
{
A = a;
}
void setb(double B)
{
B = b;
}
void sety(double Y)
{
Y = y;
}
};
int main()
{
TrigMath triangle1;
triangle1.seta(3);
triangle1.setb(4);
triangle1.sety(60);
cout << "c is equal to " << triangle1.LawOfCos() << endl;
return 0;
}
The cos() function there takes input as radians not as degrees.
Try to convert degrees to radians and then supply it as input.
In the class functions seta, setb and sety you have written A = a, B = b and Y = y.
You have to change them to a = A, b = B and Y = y.
So after applying all the changs the code should be like
#include <iostream>
#include <cmath>
using namespace std;
class TrigMath
{
private:
double a = 0;
double b = 0;
double y = 0;
public:
double LawOfCos()
{
return sqrt(pow(a,2) + pow(b,2) - 2*a*b*cos(y));
}
void seta(double A)
{
a = A;
}
void setb(double B)
{
b = B;
}
void sety(double Y)
{
y = Y*3.14/180;
}
};
int main()
{
TrigMath triangle1;
triangle1.seta(3.0);
triangle1.setb(4.0);
triangle1.sety(60.0);
cout << "c is equal to " << triangle1.LawOfCos() << endl;
return 0;
}
I learned how to pass member functions to another member function as a function pointer argument.
Now, I'm trying to pass lamda as a function pointer argument of member function.
My Code:
#include <iostream>
using namespace std;
class Test
{
public:
int add(int a, int b)
{
return a + b;
}
int sub(int a, int b)
{
return a - b;
}
typedef int (Test::*funcPtr)(int a, int b);
int myFunc(funcPtr func, int a, int b)
{
return (this->*func)(a, b);
}
void setup()
{
cout << myFunc(&Test::add, 5, 3) << endl;
cout << myFunc(&Test::sub, 5, 3) << endl;
cout << myFunc([](int a, int b) {return a * b;}, 5, 3) << endl; //ERROR!!!
}
};
int main()
{
Test test;
test.setup();
}
Result:
Error: : No viable conversion from lambda to 'Test::funcPtr' (aka 'int
(Test::*)(int, int)')
Expected Result:
8
2
15
How should I correct my code so I can get the expected result?
one option is making your functions static and then use std::function as type:
using funcType = std::function<int(int, int)>;
int myFunc(funcType func, int a, int b)
{
return func(a, b);
}
void setup()
{
cout << myFunc(Test::add, 5, 3) << endl;
cout << myFunc(Test::sub, 5, 3) << endl;
cout << myFunc([](int a, int b) {return a * b;}, 5, 3) << endl;
}
Live
Thanks to #holyBlackCat the other option is using regular function pointer(member functions need to be static):
typedef int (*funcPtr)(int a, int b);
//or:
//using funcPtr = int (*)(int a, int b);
int myFunc(funcPtr func, int a, int b)
{
return (*func)(a, b);
}
and also template:
template<typename funcType>
int myFunc(funcType func, int a, int b)
{
return func(a, b);
}
void setup()
{
cout << myFunc(Test::add, 5, 3) << endl;
cout << myFunc(Test::sub, 5, 3) << endl;
cout << myFunc([](int a, int b) {return a * b;}, 5, 3) << endl;
}
regular function pointer live, template live
EDIT
The examples provided above are working only with static member function. To invoke non-static member functions you can use pointer to member function type
using funcPtr = int(Test::*)(int a, int b);
int myFunc(funcPtr func, int a, int b)
{
return invoke(func, this, a, b);
}
//..
// call:
cout << myFunc(&Test::add, 5, 3) << endl;
pointer to non-static member function live
Im trying to use stable_sort in order to sort a vector of pointers
to a certain class. I've a code like this :
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class B
{
public :
B(int y, int j) {x = y, r = j;};
void getVal() {cout << x << endl; };
int x;
int r;
};
bool compareB(B* b1, B* b2)
{
return b1->getVal() < b2->getVal();
}
int main()
{
B b1(3, 4), b2(-5, 7), b3(12, 111);
vector<B*> myVec;
myVec.push_back(&b1);
myVec.push_back(&b2);
myVec.push_back(&b3);
std::stable_sort(myVec.begin(), myVec.end(), compareB);
for (size_t size = 0; size < myVec.size(); ++size)
{
myVec[size]->getVal();
}
return 0;
}
However, I get the foolowing error while compiling it :
"error: invalid operands of types 'void' and 'void' to binary 'operator<'
return b1->getVal() < b2->getVal();"
Can someone help me ?
The problem is with
void getVal() {cout << x << endl; };
It returns void instead of some value.
When you use it in return b1->getVal() < b2->getVal(); it boils down to return void < void; which will not compile.
You should be able to change it to
int getVal() { return x; };
I am trying to grasp pointer function concept in a better way. So I have a very simple and working example as:
#include <iostream>
using namespace std;
int add(int first, int second)
{
return first + second;
}
int subtract(int first, int second)
{
return first - second;
}
int operation(int first, int second, int (*functocall)(int, int))
{
return (*functocall)(first, second);
}
int main()
{
int a, b;
int (*plus)(int, int);
int (*minus)(int, int);
plus = &add;
minus = &subtract;
a = operation(7, 5, add);
b = operation(20, a, minus);
cout << "a = " << a << " and b = " << b << endl;
return 0;
}
So far so good,
Now I need to group the functions in a class, and select add or subtract based on the function pointer that i use. So I just make a small modification as:
#include <iostream>
using namespace std;
class A
{
public:
int add(int first, int second)
{
return first + second;
}
int subtract(int first, int second)
{
return first - second;
}
int operation(int first, int second, int (*functocall)(int, int))
{
return (*functocall)(first, second);
}
};
int main()
{
int a, b;
A a_plus, a_minus;
int (*plus)(int, int) = A::add;
int (*minus)(int, int) = A::subtract;
a = a_plus.operation(7, 5, plus);
b = a_minus.operation(20, a, minus);
cout << "a = " << a << " and b = " << b << endl;
return 0;
}
and the obvious error is:
ptrFunc.cpp: In function ‘int main()’:
ptrFunc.cpp:87:29: error: invalid use of non-static member function ‘int A::add(int, int)’
ptrFunc.cpp:88:30: error: invalid use of non-static member function ‘int A::subtract(int, int)’
coz I haven't specified which object to invoke(and I don't want to use static methods for now)
EDIT:
several comments and answers suggested that the non-static version(as I have written) is not possible.(thanks to all)
So,
Modifying the class in the following manner also wont work:
#include <iostream>
using namespace std;
class A
{
int res;
public:
A(int choice)
{
int (*plus)(int, int) = A::add;
int (*minus)(int, int) = A::subtract;
if(choice == 1)
res = operation(7, 5, plus);
if(choice == 2)
res = operation(20, 2, minus);
cout << "result of operation = " << res;
}
int add(int first, int second)
{
return first + second;
}
int subtract(int first, int second)
{
return first - second;
}
int operation(int first, int second, int (*functocall)(int, int))
{
return (*functocall)(first, second);
}
};
int main()
{
int a, b;
A a_plus(1);
A a_minus(2);
return 0;
}
generated this error:
ptrFunc.cpp: In constructor ‘A::A(int)’:
ptrFunc.cpp:11:30: error: cannot convert ‘A::add’ from type ‘int (A::)(int, int)’ to type ‘int (*)(int, int)’
ptrFunc.cpp:12:31: error: cannot convert ‘A::subtract’ from type ‘int (A::)(int, int)’ to type ‘int (*)(int, int)’
may I know how to solve this issue please?
thanks
The syntax to declare a function pointer to member methods is:
int (A::*plus)(int, int) = &A::add;
int (A::*minus)(int, int) = &A::subtract;
To invoke member methods use .* or ->* operator:
(a_plus.*plus)(7, 5);
Also have a look at http://msdn.microsoft.com/en-us/library/b0x1aatf(v=vs.80).aspx
Hope this helps.
Complete code:
#include <iostream>
using namespace std;
class A
{
public:
int add(int first, int second)
{
return first + second;
}
int subtract(int first, int second)
{
return first - second;
}
int operation(int first, int second, int (A::*functocall)(int, int))
{
return (this->*functocall)(first, second);
}
};
int main()
{
int a, b;
A a_plus, a_minus;
int (A::*plus)(int, int) = &A::add;
int (A::*minus)(int, int) = &A::subtract;
a = a_plus.operation(7, 5, plus);
b = a_minus.operation(20, a, minus);
cout << "a = " << a << " and b = " << b << endl;
return 0;
}
You can't pass non-static member function as argument that easy. And for your needs, I believe it's better to override operators: http://www.learncpp.com/cpp-tutorial/92-overloading-the-arithmetic-operators/
But if you really need them as actual member functions - just make them static.
The edit you made to your code is still wrong because it doesn't make the member functions static. You need to make the add, subtract etc. functions static by adding the static specifier:
#include <iostream>
using namespace std;
class A
{
int res;
public:
A(int choice)
{
int (*plus)(int, int) = A::add;
int (*minus)(int, int) = A::subtract;
if(choice == 1)
res = operation(7, 5, plus);
if(choice == 2)
res = operation(20, 2, minus);
cout << "result of operation = " << res;
}
static int add(int first, int second)
{
return first + second;
}
static int subtract(int first, int second)
{
return first - second;
}
static int operation(int first, int second, int (*functocall)(int, int))
{
return (*functocall)(first, second);
}
};
See the below code. The function calls are working without making them static.
class A
{
public:
int add(int first, int second)
{
return first + second;
}
int subtract(int first, int second)
{
return first - second;
}
int operation(int first, int second, int(A::*functocall)(int, int))
{
return (this->*functocall)(first, second);
}
};
//typedef int(A::*PFN)(int, int) ;
int main()
{
int a, b;
A a_plus, a_minus;
a = a_plus.operation(7, 5, &A::add);
b = a_minus.operation(20, a, &A::subtract);
cout << "a = " << a << " and b = " << b << endl;
return 0;
}