Function overloading error

I am learning function overloading and i decided to make this simple program

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
void add(int a,int b);
void add(float a,float b);
void add(double a,double b);
void main()
{


int c;
cout<<"1.Add integers\n2.Add real numbers\n3.Add double integer\nEnter your choice: ";
cin>>c;
if(c==1)
  int a,b;
else if(c==2)
  float a,b;
else if(c==3)
  double a,b;

cout<<"enter two numbers to add";
cin>>a,b;
cout<<"the sum is ";
add(a,b);


}
void add(int a,int b)
{
cout<<a+b;
}
void add(float a,float b)
{
cout<<a+b;
}
void add(double a,double b)
{
cout<<a+b;
}


But I am getting the following errors


Unidentified symbol a and b in function main()


what is wrong with my code?
Last edited on
Your scoping is inappropriate.

Declare your variable like this:
int a,b,c

Make separate sets of variable of float data type.
cin>>a,b;

Cant do that. One at a time sir. Or do it like this cin >> a >> b;

Also, change it to int main()


But the main reason you're getting that error is this -
1
2
3
4
if(c==1)
  int a,b;
else if(c==2)
  float a,b;


If you're variable is created within if statements. You cant just do add(a,b); Because they're not defined. It makes no sense that you want to create variables only IF something happens. Declare them at the very top.
Last edited on
oh my god! just missed that cin>>a,b :D
Thanks @TarikNeaj
Last edited on
@abeginner23235616

how do I make different sets of variable

If I do so I would need many add() function call

Then what will be the point of using overloading? We could simply make different functions eh?
@Programmer007. Do it like this.

First.
1
2
3
int c;
cout<<"1.Add integers\n2.Add real numbers\n3.Add double integer\nEnter your choice: ";
cin>>c;


Thats all good. Then, do this

You can create variables, and make them enter 2 numbers. Then you can do if statements like yours. And if c == 1, then call this function. If c == 2, call that function etc.
Is it necessary to name your float variables also as a and b;
Give them a different name(say h and j), declare them after int main().

Then you can call them
add(h,j)

Do the same thing for the double variables.
Yep, what beginner said. Something like this -

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
void add(int a, int b);
void add(float a, float b);
void add(double a, double b);
int main() // int main buddy
{
	int a, b;
	float c, d;
	double e, f;

	cout << "1.Add integers\n2.Add real numbers\n3.Add double integer\nEnter your choice: ";
	cin >> c;

	if (c == 1) {
		cout << "enter two numbers to add";
		cin >> a >> b;
		cout << "the sum is ";
		add(a, b);
	}
		
	else if (c == 2){
		cout << "enter two numbers to add";
		cin >> c >> d;
		cout << "the sum is ";
		add(c, d);
	}
		
	else if (c == 3){
		cout << "enter two numbers to add";
		cin >> e >> f;
		cout << "the sum is ";
		add(e, f);
	}
		

	system("pause");


}
void add(int a, int b)
{
	cout << a + b << endl;
}
void add(float a, float b)
{
	cout << a + b << endl;
}
void add(double a, double b)
{
	cout << a + b << endl;
}

thanxx @abeginner23235616 and @TarikNeaj

I did it somewhat like this

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
void add(int a,int b);
void add(float a,float b);
void add(double a,double b);
int main()
{


int c;
cout<<"1.Add integers\n2.Add real numbers\n3.Add double integer\nEnter your choice: ";
cin>>c;
if(c==1)
  {
  int i1,i2;

  cin>>i1>>i2;
  add(i1,i2);
  }
else if(c==2)
  {
  float f1,f2;

  cin>>f1>>f2;
  add(f1,f2);
  }
  else if(c==3)
  {
  double d1,d2;
  add(d1,d2);
  }

return 0;
}
void add(int a,int b)
{
cout<<a+b;
}
void add(float a,float b)
{
cout<<a+b;
}
void add(double a,double b)
{
cout<<a+b;
}//problem solved :D


But still wondering whats wrong with this?

1
2
3
4
5
6
if(c==1)
  int a,b;
else if(c==2)
  float a,b;
else if(c==3)
  double a,b;
Last edited on
Look up what scope means. The if statement has its own scope.

1
2
3
4
if(this)
{
  // that
}


Everything declared inside the if statement, as far as I know, does not exist outside of it. So when you do.

1
2
if(c==1)
  int a,b;


And then try to do cin>>a,b; outside of the if statement, int a,b; does not exist anymore.

Edit: Also, in the program you just showed, you forgot the input here

1
2
3
4
5
else if(c==3)
  {
  double d1,d2;
  add(d1,d2);
  }
Last edited on
@TarrikNeaj

thanks man...... got it :D
It's a scope issue.

Variables cease to exist when they leave their scope. Usually this is the enclosing {braces}, but in your case it is the if statements.

1
2
3
4
5
6
7
8
9
10
// This code:
if(c==1)
  int a,b;


// Is 100% identical to this code:
if(c==1)
{
    int a,b;
}


And this highlights the scope of these variables:
1
2
3
4
5
if(c==1)
{
    int a,b;  // < a,b have scope only in their enclosed braces
} // <- a,b go out of scope here
//< a,b no longer exist here 


Therefore:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
if(c==1)
  int a,b;
// <- a,b goes out of scope and no longer exist here
else if(c==2)
  float a,b;  //<- this creates DIFFERENT variables named a,b
// <- ..which promptly go out of scope and no longer exist
else if(c==3)
  double a,b; //<- yet another set of vars
// <- ..which go out of scope

// <- at this point in the program, all variables named a,b have gone out of scope
//  and therefore none of them exist
cout<<"enter two numbers to add";
cin>>a,b;  // <-  so this line gives you an error, because a,b don't exist 



If you want a,b to exist throughout main, then you will have to give them scope inside of main.


Note that you cannot change their type based on user input. IE: you can't make 'a' an int if the user inputs 1 and a float if they input 2. Once you decide to make 'a' an int, it is always an int.

This means you will likely need to have a different set of variables (possibly with different names) for each type. Or use templates -- but that's probably more advanced than you need.



EDIT:

Also...
cin>>a,b;

This is wrong anyway. General rule: don't use the comma operator for anything other than argument/parameter lists -- it never does what you think it does. It's the single most confusing and useless operator in C++.

You meant to do this:
cin >> a >> b;


EDIT: holy crap did I get ninja'd. I didn't see any replies before. Maybe I left this window open way too long before responding. =x
Last edited on
just brushed my code up a bit

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
53
54
55
56
57
58
59
60
void show();
void result();
void add(int a,int b);
void add(float a,float b);
void add(double a,double b);
int main()
{


int c;
cout<<"1.Add integers\n2.Add real numbers\n3.Add double integer\nEnter your choice: ";
cin>>c;
if(c==1)
  {
  int i1,i2;
  show();
  cin>>i1>>i2;
  result();
  add(i1,i2);
  }
else if(c==2)
  {
  float f1,f2;
  show();
  cin>>f1>>f2;
  result();
  add(f1,f2);
  }
  else if(c==3)
  {
  show();
  double d1,d2;
  cin>>d1>>d2;
  result();
  add(d1,d2);
  }

return 0;
}
void show()
{
cout<<"Enter two numbers: ";
}
void result()
{
cout<<"The result is ";
}
void add(int a,int b)
{
cout<<a+b;
}
void add(float a,float b)
{
cout<<a+b;
}
void add(double a,double b)
{
cout<<a+b;
}

now should I use inline for every function? ( i.e. add() show() and result() )

my teacher keeps nagging me to do it but i don't get the point..
Last edited on
I dont know what inline does but. Who's idea what is to have 2 functions, to just print out 3 words each? Thats absurdly unnecessary :D
I don't think inline is needed for a simple program like this. People use inline when they want to save memory. When you make a bulky program, which consumes lot of speed and memory, maybe using 'inline' functions will help you.
But not this time.
@TarikNeaj

It helps in the long run.... :D

Besides I am a bit lazy......... :D

@abeginner23235616

So exactly where is inline used?
Last edited on
To clarify inline:


Function inlining is an optimization technique that compilers sometimes use. The idea is, instead of the traditional function call where your program jumps to another part of the program, an inlined function will effectively be copy-pasted into whatever code called it.

This has some benefits:
- It saves the overhead involved with calling a new function
- It allows for further optimization of the function in the context of the calling code

But also some drawbacks:
- It can create larger code, since you are duplicating the function body everywhere that calls it



The inline keyword is sort of a hint to the compiler that you want this function to be inlined when called. However, it does not guarantee that it will be. In fact, compilers may outright ignore this keyword and simply use their best judgement when determining whether or not the function will be inlined.



In practice, the inline keyword has a different meaning altogether, and more closely relates to some linking rules - and has little to do with whether or not a function is actually inlined.



In short: don't bother with the inline keyword unless you need it. Let the optimizer optimize for you. In this case, you do not need to mark anything as inline.
thanxx everybody :D
Topic archived. No new replies allowed.