954,505 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

error C2064: term does not evaluate to a function taking 1 arguments

I am new to C++. This is a homework problem. So if anyone gives up their time to help me it is greatly appreciated. I have perfect number program, I can get it to run if all my code is in the main. The assignment requests that I use a function to check numbers to see if they are perfect. I am getting an error when I use the function. I checked MSDN to find some help to no avail. So here is the code. Thanks again for any help!:cheesy:

#include <iostream>
using std::cin;
using std::cout;
using std::endl;

int perfect (int);

int main ()
{
    int number = 1;
    int perfect = 0;
    
    for(number=1; number < 1000; number++)
    {
        perfect = perfect(number);  //here is where I call the function
        if (perfect > 0)
        {
            cout << perfect << " is a perfect number." << endl;
            cout << "It's factors are: ";

            for ( int y = 1; y < perfect/2; y++ )
            {
                int divisor = perfect / y;
                
                if ( perfect % y == 0 && y <= divisor)
                {    
                    cout << divisor << " " << y << endl;
                }
            } // ends factor perfect
        } // ends if perfect
    } // ends for to 1000
}// ends main

    int perfect (int counter)  //here is where the function starts
{
    int sum = 0;
    int divisor = 0;
    int y = 0;
    for (y = 1; y < counter/2; y++)
    {
        divisor = counter / y;
        if ( counter % y == 0 && y <= divisor)
        {
            int factors = y + divisor;
            sum += factors;
        }
    }
    if (sum - counter == counter)
    {
        return counter;
    }
    else
        return 0;
}//ends function
dmmckelv
Light Poster
33 posts since Nov 2006
Reputation Points: 10
Solved Threads: 0
 

Hello,

The problem is, your function is called 'perfect' and your variable is too. You should change one of the two like so:

#include<iostream>
using std::cin;
using std::cout;
using std::endl;
 
int perfect (int);
 
int main ()
{
int number = 1;
int p = 0; //instead of perfect
//etc....
Nick Evan
Not a Llama
Moderator
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
 

yep thats fine.. also make sure you return 0 when you declare int main(), otherwise declare void main()
The final program should look like this: (I changed the name of the function perfect(...) to perfect_function(...) and i added a return 0 at the end of main)

#include <iostream>
using std::cin;
using std::cout;
using std::endl;
 
int perfect_function (int);
 
int main ()
{
    int number = 1;
    int perfect = 0;
    
    for(number=1; number < 1000; number++)
    {
        perfect = perfect_function(number);  //here is where I call the function
        if (perfect > 0)
        {
            cout << perfect << " is a perfect number." << endl;
            cout << "It's factors are: ";
 
            for ( int y = 1; y < perfect/2; y++ )
            {
                int divisor = perfect / y;
                
                if ( perfect % y == 0 && y <= divisor)
                {    
                    cout << divisor << " " << y << endl;
                }
            } // ends factor perfect
        } // ends if perfect
    } // ends for to 1000
    return 0;
}// ends main
 
int perfect_function (int counter)  //here is where the function starts
{
    int sum = 0;
    int divisor = 0;
    int y = 0;
    for (y = 1; y < counter/2; y++)
    {
        divisor = counter / y;
        if ( counter % y == 0 && y <= divisor)
        {
            int factors = y + divisor;
            sum += factors;
        }
    }
    if (sum - counter == counter)
    {
        return counter;
    }
    else
        return 0;
}//ends function
may4life
Junior Poster in Training
57 posts since Oct 2006
Reputation Points: 13
Solved Threads: 2
 
otherwise declare void main()


Nope. Just use return 0;

Nick Evan
Not a Llama
Moderator
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
 
... otherwise declare void main()

main() is anint function and officially cannot be be declared as void. In fact, some compilers flag a warning if void is used. Forget what M$ claims in their help. They are wrong. :confused:

WaltP
Posting Sage w/ dash of thyme
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

return 0 returns the number 0 (obviously.. hehe) to the Operating System. This is used to denote that the function main() was successfully completed. You can use void main() but as my friends above also noted, it is not recommended... so my final thoughts...DO use

int main()
...
return 0; // to the OS
may4life
Junior Poster in Training
57 posts since Oct 2006
Reputation Points: 13
Solved Threads: 2
 
perfect = perfect(number); //here is where I call the function


Well, the problem is that you have a variable (int perfect), as well as a function named perfect, which is not allowed, so you can either change the variable name, or the function name

venkat arun
Light Poster
28 posts since Oct 2009
Reputation Points: 4
Solved Threads: 1
 
Well, the problem is that you have a variable (int perfect), as well as a function named perfect, which is not allowed, so you can either change the variable name, or the function name

How is that any different from whatI said 3 years ago in this same thread?

The problem is, your function is called 'perfect' and your variable is too. You should change one of the two

Lesson learned: Read thread first, reply later!

Nick Evan
Not a Llama
Moderator
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You