Not able to call a function using function pointer [duplicate] - c++

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to call a function using pointer-to-member-function
Analyzer.h
class Analyzer
{
public :
void viku();
void Bibek();
void vivek();
void (Analyzer::*point)();
Analyzer(){
}
~Analyzer(){
}
};
Analyzer.cpp
using namespace std
#include"Analyzer.h"
void Analyzer::viku(){
cout<<"Hello viku";
}
void Analyzer::vivek(){
point =&Analyzer::viku;
Bibek();
}
void Analyzer::Bibek(){
point();//Errror
cout<<"Bibek";
}
During compilation it shows the following error:
error C2064: term does not evaluate to a function taking 0 arguments.
Can anyone please tell me how to avoid this?

Pointers to member functions are different than normal function pointer. You need an instance to call them:
#include <iostream>
class A
{
public:
int foo()
{
std::cout << "A::foo here, you can have 42" << std::endl;
return 42;
}
};
int main ()
{
int (A::* point)() = &A::foo;
A a;
(a.*point)();
}
In your case, you'd need to do something like the following:
(this->*point)()

Related

About function declarations in functions [duplicate]

This question already has answers here:
Is there a use for function declarations inside functions?
(4 answers)
C++: Why does function declaration is allowed inside another function but not function definition?
(1 answer)
Why does a function declaration within another function compile and what does it do?
(2 answers)
Closed 7 months ago.
We can have function declarations inside of function bodies:
void f(double) {
cout << "f(double) called";
}
void f(int) {
cout << "f(int) called";
}
void g() {
void f(double); //Functions declared in non-namespace scopes do not overload
f(1); //Therefore, f(1) will call f(double)
}
This can be done in order to hide a function such as f(int) in this case, but my question is: Is this really the only reason why it is possible to declare a function inside of another?
Or in other words:
Does the possibility of declaring a function inside of a function exist for the sole purpose of hiding a function?
This can also be used to limit the visibility of a function declaration to only one specific function. For example:
file1.cpp:
#include <iostream>
void f(double d) {
std::cout << d << '\n';
}
file2.cpp:
int main() {
void f(double);
f(1); // prints 1
}
void g() {
f(2); // error: 'f' was not declared in this scope
}

C++ compiling error when calling a constructor within other constructor's call [duplicate]

This question already has an answer here:
Why "Foo f(Bar());" can be a declaration of a function that takes type Bar and returns type Foo? [duplicate]
(1 answer)
Closed 4 years ago.
I have a C++ code that seems to be confusing a class contructor like A::A(B b) with a constructor that receives a function pointer, like A::A(B (*)()). Let me explain:
The following code compiles:
#include <iostream>
#include <cstring>
#include <vector>
struct Item {
Item() {
std::cout << "ITEM::Normal constructor\n";
}
};
struct Container {
Container(Item i) {
std::cout << "CONTAINER::Normal constructor\n";
}
void doSomething() {
std::cout << "Do something\n";
}
};
int main() {
Container c3(Item());
return 0;
}
But if I add a call to B::doSomething(), like the following code, I obtain a compiler error that I don't understand:
#include <iostream>
#include <cstring>
#include <vector>
struct Item {
Item() {
std::cout << "ITEM::Normal constructor\n";
}
};
struct Container {
Container(Item i) {
std::cout << "CONTAINER::Normal constructor\n";
}
void doSomething() {
std::cout << "Do something\n";
}
};
int main() {
Container c3(Item());
c3.doSomething();
return 0;
}
The compiling error is:
main.cpp: In function ‘int main()’:
main.cpp:23:6: error: request for member ‘doSomething’ in ‘c3’, which is of non-class type ‘Container(Item (*)())’
c3.doSomething();
It's a veiled most vexing parse issue:
Container c3(Item());
declares a function prototype, and your helpful compiler issues the appropriate diagnostic.
Container c3{Item()};
is the fix.

Why class defination after main() function will not work? [duplicate]

This question already has answers here:
C++ what to code if i put a class after main() function
(5 answers)
Closed 7 years ago.
If the function is defined after main() function program will work...
void printdata(int i);
int main()
{
printdata(20);
return 0;
}
void printdata(int i)
{
std::cout << "i = " << i << std::endl;
}
If we declare class before main function and defined after main function, why it will through error?
#include <iostream>
class C;
int main()
{
C c(20);
c.printdata();
return 0;
}
class C
{
int i;
public:
C(int a) : i(a) {};
void printdata()
{
std::cout << "C:i = " << i << std::endl;
}
};
Error log after compiling the code:
class_after_main.cpp: In function ‘int main()’:
class_after_main.cpp:7:6: error: variable ‘C c’ has initializer but incomplete type
C c(20);
If you declare a class without defining it, you can only use reference or pointer to that class. The compiler needs to know the size of the class to define the size needed to store its objects.

C++11 multithreading with class member function [duplicate]

This question already has answers here:
Start thread with member function
(5 answers)
Closed 7 years ago.
I want to use multithreading in C++11 to call a class member function in its own thread. I have been able to get this to work with a global function:
#include <thread>
#include <iostream>
void Alpha(int x)
{
while (true)
{
std::cout << x << std::endl;
}
}
int main()
{
std::thread alpha_thread(Alpha, 5);
alpha_thread.join();
return 0;
}
However, I cannot get it to compile with a class member function:
#include <thread>
#include <iostream>
class Beta
{
public:
void Gamma(int y)
{
while (true)
{
std::cout << y << std::endl;
}
}
};
int main()
{
Beta my_beta;
std::thread gamma_thread(my_beta.Gamma, 5);
gamma_thread.join();
return 0;
}
The compile error is:
no matching function for call to 'std::thread::thread(<unresolved overloaded function type>)'
std::thread gamma_thread(my_beta.Gamma, 5);
^
What am I doing wrong?
You need to pass two things: a pointer-to-member, and the object. You cannot call a non-static member function (like Gamma) in C++ without an object. The correct syntax would be:
std::thread gamma_thread(&Beta::Gamma, // the pointer-to-member
my_beta, // the object, could also be a pointer
5); // the argument
You can think of my_beta here as being the first argument to Gamma(), and 5 as the second.
You need to name the function, then pass the object on which to call it as an explicit implicit this parameter. :)
std::thread gamma_thread(&Beta::Gamma, my_beta, 5);
This is a bit of an abstraction leak, granted.
You have multiple problems in your program
As your compile error says, you need to pass the address of the function &Beta::Gamma.
You need to pass the object as a parameter considering this is an implicit parameter of a member function
Modified source
#include <thread>
#include <iostream>
class Beta
{
public:
void Gamma(int y)
{
while (true)
{
std::cout << y << std::endl;
}
}
};
int main()
{
Beta my_beta;
std::thread gamma_thread(&Beta::Gamma, my_beta, 5);
gamma_thread.join();
return 0;
}

How to pass a non-static member function as a unique_ptr deleter [duplicate]

This question already has answers here:
How Can I Pass a Member Function to a Function Pointer?
(3 answers)
Closed 7 years ago.
#include <memory>
#include <iostream>
#include <exception>
#include <curl/curl.h>
class client
{
private:
std::unique_ptr<CURL, decltype(&psclient::del_curl)> uptr_curl_;
inline CURL * init_curl()
{
CURLcode result = curl_global_init(CURL_GLOBAL_DEFAULT);
if(result != CURLE_OK)
throw std::logic_error(curl_easy_strerror(result));
return curl_easy_init();
}
inline void del_curl(CURL * ptr_curl)
{
curl_easy_cleanup(ptr_curl);
curl_global_cleanup();
}
public:
inline client()
: uptr_curl_(init_curl(), &client::del_curl)
{
}
}
The compiler keeps complaining No matching constructor for initialization of 'std::unique_ptr<CURL, void (*)(CURL *)>'
It seems to me like the declaration is correct for the deleter template argument. It is a function pointer that returns void and takes a CURL * as an argument. This matches the signature of del_curl.
Is there yet another random rule, unknown to me, in C++ that specifies a requirement for template arguments to non-static member function pointers? If so, why?
The answer of #R. Sahu is correct imo. However, if you insist of passing a non-static member function deleter, here is a way of doing it using the good old std::bind and std::function:
#include <memory>
#include <iostream>
#include <functional>
class Foo
{
private:
std::unique_ptr<int, std::function<void(int*)>> _up;
public:
Foo(): _up(new int[42], std::bind(&Foo::deleter, this, std::placeholders::_1))
{
}
void deleter(int* p)
{
delete[] p;
std::cout << "In deleter" << std::endl;
}
};
int main()
{
Foo foo;
}
PS: I just don't like the bind, I wonder if one can improve on that.
With a lambda:
Foo(): _up(new int[42],
[this](int* p)->void
{
deleter(p);
}
){}
The second template parameter used in the declaration of uptr_curl_ is void (*)(CURL *)
The type of &client::del_curl is void (CURL::*)(CURL*).
They are not the same. You can change del_curl to a static member function. That will resolve the problem.
Update
You can use a non-static member function with the help of std::function and std::bind.
class client
{
public:
client();
private:
std::unique_ptr<CURL, std::function<void(CURL *)>> uptr_curl_;
CURL * init_curl();
void del_curl(CURL * ptr_curl);
};
client::client() : uptr_curl_(init_curl(),
std::bind(&client::del_curl, this, std::placeholders::_1))
{
// ...
}