Solving error “Expression preceding parentheses of apparent call must have (pointer-to-) function type” In C++

Expression preceding parentheses of apparent call must have (pointer-to-) function type

If you are encountering the error “expression preceding parentheses of apparent call must have (pointer-to-) function type” in C++ and has difficulties fixing it. Now, let’s follow this guide with the explanation and examples below.

How does the error “expression preceding parentheses of apparent call must have (pointer-to-) function type” happen in C++?

Many causes make this error happen. But these are some common causes that many programmers encounter:

  • Missing the colon after the access modifier.
  • Missing required header inclusion and using.
  • Trying to call a function that is not defined as a function.

Look at the example below.

#include <iostream>

template <typename T>
class Fraction {
    int numerator ;
    int denominator;
    Fraction() {};
public  // Missing the colon after the access modifier.
    Fraction(int a, int b) :numerator(a), denominator(b) {};
    void exportFraction const() { 
        cout << numerator<<"/"<<denominator;  // Missing required header inclusion and using.
    };
};

int main() {
    Fraction<int> f(3,4);
    f.exportFraction(); // Trying to call a function that is not defined as a function.
}

Output

Expression preceding parentheses of apparent call must have (pointer-to-) function type

How to solve this error?

Adding the colon after the access modifier

Review your code carefully and be sure that there is a colon after the access modifier (private, public, protected).

private:

protected: 

public:

Adding required header inclusion and using

You can add the specified function by using command:

Syntax

using std:: theNameOfTheFucntion;

Look at the example below.

using std::cout;
using std::endl;

Or you can declare using the std namespace. It has almost all the functions that you need for coding.

Syntax

using namespace std;

Rewriting the function properly

Check if your function is correct or not. The parentheses are after the name of the function.

Syntax:

type name(parameters)

Parameters:

  • type: The return type.
  • name: The name of the function.
  • parameters: Optional. The parameters of the function.

Example:

void exportFraction() 

Look at the standard code after using these solutions.

#include <iostream>
using namespace std;

template <typename T>
class Fraction {
    int numerator ;
    int denominator;
    Fraction() {};
public:
    Fraction(int a, int b) :numerator(a), denominator(b) {};
    void exportFraction() const { 
        cout << numerator<<"/"<<denominator; 
    };
};

int main() {
    Fraction<int> f(3,4);
    f.exportFraction();
}

Output:

3/4 

Summary

In this article, we have given you some ways to fix the error “Epression preceding parentheses of apparent call must have (pointer-to-) function type”. We hope it is helpful to you. If you have any questions or problems, please comment below. Thanks for reading!

Maybe you are interested:

Posted in C++

Leave a Reply

Your email address will not be published. Required fields are marked *