### Calculating the average using two different functions.

When I compile my code with g++ I get the error: constructor,destructure or type convertion missing before sum.

int sum(int,int);
void sum(int,int,int&);

main()
sum (v) // This is the line that the compiler is reporting the error occurs.
answer sum()

int main(){
int a=1;
int b=2;
int sum;

sum=sum(a,b);
print(a,b,sum);
sum(a,b,sum);

print(a,b,sum);
return 0;
}

int sum(int n1,int n2){
return n1+n2;
}

void sum(int n1,int n2,int& answer){
answer=n1 + n2;
}

void print(int v1,int v2,int sum){
cout<<v1<<"+"<<v2<<"="<<sum<<endl;
Last edited on
There seem to be many, many problems with this code.
I have no idea what sum[v] is supposed to be, if it is a forward declaration of the sum function it should have parentheses () not brackets.

The function sum may need assignment modifiers (ie a=1 not a1, unless you mean to refer to a variable "a1" earlier in the code, or if you're trying to multiply them it should be a*1. If you want to assign the value to a of a times 1 then it should be a*=1 or a=a*1, which are equivalent.

You're also missing a parentheses (second one) on the function sum which may be the source of the error message. Also I believe you need return statements, ie return(); or return(0); You could also use return (answer); but I assume you're practicing passing variable references.
I made corrections and still get the same error message.
This entire chunk makes no sense, what-so-ever:
 main() //This is the part returning the error sum (v) answer sum()

What do you think that each line is supposed to do (from what I have quoted)? Maybe if you explain that, we can better help you.

Also:
-In the parameter list for the second sum function, you missed a space between int and n2,
-You are missing a brace at the end of your print function,
-You don't provide a function prototype for your print function,
-In main, you declare a variable named sum. This is an issue because you have a function named sum in the global scope that is "hidden" (if you will) in the main function by the local sum variable.

After fixing the above, I got it to compile and run.

 The function sum may need assignment modifiers (ie a=1 not a1, unless you mean to refer to a variable "a1"
`int sum(int a1, int b1)` As you can see, a1 and b1 are parameter names (read: variables).

 Also I believe you need return statements, ie return(); or return(0); You could also use return (answer); but I assume you're practicing passing variable references.

I would assume that the OP understands return statements, as he/ she has properly used them in the first sum function. The second sum function returns void (read: nothing), and has a reference variable answer, so you would be right in saying that the OP is practicing using references.
Last edited on
Also if you want help tell us what line(s) the error(s) are on.
 Also if you want help tell us what line(s) the error(s) are on.

^This^

Also, please use code tags: http://www.cplusplus.com/articles/z13hAqkS/
I am just a beginner at c++ so if you can show me how you got it to compile and run it sure would help me.
We're trying to help you, you need to help us help you. Does your compiler tell you what line(s) the errors relate to?
Danny Toledo said that he fixed the problems and got it to compile and run. He includes a list of problems and I corrected the ones that I could. How do you do a function prototype for the print function?
http://www.cplusplus.com/doc/tutorial/functions2/

See the section (at the bottom of the above link) Declaring functions
I am lost on how to do it. I tried and cant do it correctly.
 ``12345678910111213141516171819202122232425262728293031323334`` ``````#include using namespace std; int sum(int,int); void sum(int,int,int&); void print(int,int,int); // <- this is the function prototype for print int main(){ int a=1; int b=2; int sum; sum=::sum(a,b); print(a,b,sum); a = 3; // I changed these to show that the value of sum was indeed being b = 10; // changed after the next function call ::sum(a,b,sum); print(a,b,sum); return 0; } int sum(int a1,int b2){ return a1+b2; } void sum(int n1,int n2,int& answer){ answer=n1 + n2; } void print(int v1,int v2,int sum){ cout<
Thank you very much.
Topic archived. No new replies allowed.