Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7930255
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T20:21:56+00:00 2026-06-03T20:21:56+00:00

I’m trying to call functions through their pointers. Compiling this code, I get errors

  • 0

I’m trying to call functions through their pointers. Compiling this code, I get errors saying that expressions of type void cannot be converted to other types, but I check whether the function returns a void or not before calling the function. Is there another way that I can accomplish this?

class FuncBase {
public:
FuncBase(string n, string ret, string arg1, string arg2): name(n), retval(ret), a1(arg1), a2(arg2) {} 
string getName() const { return name; }
string getRet() const { return retval; }
string getA1() const { return a1; }
string getA2() const { return a2; }
virtual void call() = 0; 

private: 
string name, retval, a1, a2; 
};  

template <typename ret, typename arg1, typename arg2> 
class Func: public FuncBase {
public:
Func(string n, string r, string ar1, string ar2, ret(*fc)(arg1, arg2)):
                FuncBase(n, r, ar1, ar2), func(fc) {}
void call() {
    arg1 ar1;
    arg2 ar2;
    cout << "You chose the " << getName() << " function" << endl; 
    cout << "Enter a " << getA1() << ": "; 
    cin  >> ar1;
    cout << "Enter a " << getA2() << ": "; 
    cin  >> ar2; 

    if (getRet() != "void") {
        ret val = (*func)(ar1, ar2);
        cout << getName() << " returned " << val << endl; 
    }
    else (*func)(ar1, ar2); 
    cout << endl; 
}

private:
// pointer to function
ret(*func)(arg1, arg2);
}; 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T20:21:58+00:00Added an answer on June 3, 2026 at 8:21 pm

    For template instantiations where this fails, func is a function pointer that receives two arguments and has void return type. That is ret is void. A function with a void return type does not return anything. Naturally you cannot read the return value of an invocation of (*func)() because you said that there was no return value.

    Even though the branch that reads the return value won’t get executed, it still needs to compile and pass the static type checking. The fundamental issue here is that the calls to *func() are dealt with at compile time and are subject compile time static type checking.

    This Stack Overflow question covers the exact same problem as you and the accepted answer shows how to deal with the issue

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I'm trying to create an if statement in PHP that prevents a single post
I am trying to loop through a bunch of documents I have to put
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.