any problem with this code?

#include <iostream>
add (int x, int y)

int main ()
{
using namespace std;
cout << "The sum of 3 and 4 is: " << add(3, 4) << endl;
return 0;
}

when i compile it says there is a problem with line four

int main ()

there should be an 'expected constructor, destructor or type conversion...?
Yes there is a problem. You don't have add defined, and if the beginning is a prototype, then you need a ;

Specifically change

add (int x, int y)

to

1
2
3
add (int x, int y){
//return the result of x + y here
}


or

add (int x,int y);
Last edited on
closed account (zb0S216C)
ultifinitus has pointed out the error in your code. However, declaring a method without it's body is called a prototype method. If you use a prototype method, you'll have to define it's body after main( ).

Here's an example of how to declare and define a prototype method:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// [ RETURN-TYPE ] [ IDENTIFIER ] ( [ ARGUMENT-LIST ] );
int Add( int, int ); // This is your prototype method which has not yet been defined.

int main( )
{
    Add( 3, 4 );  // This is allowed as long as you define the body of Add( ) later.
    return 0;
}

// This is the definition of Add( ):
int Add( int A, int B )
{
    return( A + B );
}
Topic archived. No new replies allowed.