visual studio not identifying problem

Hi,

for some reason i keep getting a build error, i am not sure why as the code looks fine to me. A response would be greatly appreciated. thanks.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
  #include <iostream>
using namespace std;

int high(int first, int second, int third, int fourth)
{
	int x = 0;
	int y = 0;
	int finale = 0;
	x = sum(first, second);
	y = sum(third, fourth);
	finale = sum(x, y);
	return finale;
}

int sum(int x, int y)
{
	if (x>y)
	{
		int t = 0;
		t = x;
		return t;

	}
	else
	{
		int t = 0;
		t = y;
		return t;

	}
}



int main()
{

	int sum(int, int);//function prototype 
	int high(int, int, int, int);

	int total = 0;
	total = high(2, 5, 3, 4);


	int total1 = 0;
	total1 = sum(5, 6);

	cout << total1;
	char aaa;
	cin >> aaa;
	return 0;
}
Hello southconnor,

What you have here is even though the functions come before "main" order does sometimes make a difference.

What has happened here is that the function"high" is calling the function "sum", but the compiler has yet to see this function definition.

You could change the order of the two functions or add prototypes before the functions.
Hint: In the future include the error messages you get that you do not understand, so whom ever reads the post will know what the problem is. Some will see the problem by looking at the code others, like me, may not see it right and have to load the program and compile it to see what you did not tell in your post.

I found your function prototypes in "main" by the time the compiler would reach "main" it is to late for the prototypes. they should be after the using line that is best not to use.

Once I moved the prototypes from main to above the function definitions the errors went away.

Hope that helps,

Andy
Simple solution - Order matters. Have the function "sum" be before function "high". Function high is trying to call sum but can't see it.
How odd ...
a function called sum is returning the higher of two variables ...
... and is being assigned to a variable called total1.

You really don't make life easy for yourself!
Last edited on
How about

1
2
3
4
5
6
7
8
9
int highest(int first, int second, int third, int fourth)
{
	return high(high(first,second),high(third,fourth);
}

int high(int x, int y)
{
	return (x>y)?x:y;
}


It's confusing but try analyzing it.

-> Keep in mind that function prototypes must be declared right after the definition section (where #include<headerfile> go) and in global space

-> Either that or you could put your two functions at the bottom of the program and declare your prototype inside the main itself. That works too.
Last edited on
Thanks guys this is sorted now.
Topic archived. No new replies allowed.