swap is not declared

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
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
void sort(int &small, int &mid , int &large);
void swap1(int, int);
void swap2(int, int);
int small, mid, large;
cout << "please enter a number";
cin >> small;
cout << "please enter onather number";
cin >> mid;
cout << "please enter the third number";
cin >> large;
sort (small, mid, large);
cout << "small = " << small << "middle = " << mid << "large = " << large << endl;
getch();
}
void sort (int small, int mid, int large)
{
    if (small > mid)
    swap1 (small, mid);
    if (mid > large)
    swap2 (mid, large);
}

void swap1 (int &small, int &mid)
{
int a = small;
small = mid;
mid = a;
}

void swap2 (int &mid, int &large)
{
int b = mid;
mid = large;
large = b;
}


appears that error on line 25 & 30 where swap1 & swap2 is not declared in this scope.. anyone can help?..thanx in advance
Look at the functions declaration and definition parameter types. See the difference?
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
#include <iostream>
#include <conio.h>
using namespace std;

void sort(int small, int mid , int large);
void swap1(int, int);
void swap2(int, int);
int main()
{

int small, mid, large;
cout << "please enter a number";
cin >> small;
cout << "please enter onather number";
cin >> mid;
cout << "please enter the third number";
cin >> large;
sort (small, mid, large);
cout << "small = " << small << "middle = " << mid << "large = " << large << endl;
getch();
}
void sort (int small, int mid, int large)
{
    if (small > mid)
    swap1 ( small, mid);
    if (mid > large)
    swap2 ( mid, large);
}

void swap1 (int small, int mid)
{
int a = small;
small = mid;
mid = a;
}

void swap2 (int mid, int large)
{
int b = mid;
mid = large;
large = b;
}


i've change the function sort and swap as global function...it fix the problem...but it wont swap...have any idea?
Look again at the type of parameters!
You have pass them by value which mean that intended variables wont be changed.

Lets make simple example so you can see what is happening and why
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>

void function1( int number)
{
    number = 10;
}

void function2( int& number)
{
    number = 10;
}

int main()
{
    int var = 3;

    function1( var ); // this wont change var since it is passed by value (param type is int)
    std::cout << var << std::endl; // 3 is printed
    function2( var ); // this will change var since it is passed by reference (param type is reference to int)
    std::cout << var << std::endl; // 10 is printed
};
You declared two pairs of functions

void swap1(int, int);
void swap1 (int &small, int &mid);

void swap2(int, int);
void swap2 (int &mid, int &large);


The second functions in the pairs were defined but the first functions were not defined.

At the moment of calls of the functions the compiler sees the first declarations that have no corresponding definitions. So it issues the errors.
Last edited on
@tambun

You should check a tutorial for functions. Differences between passing arguments by value, by reference or by address. Maybe this one will help:
- http://www.learncpp.com/cpp-tutorial/72-passing-arguments-by-value/
- http://www.learncpp.com/cpp-tutorial/73-passing-arguments-by-reference/
- http://www.learncpp.com/cpp-tutorial/74-passing-arguments-by-address/
Last edited on
Thank you so much guys...now I understand more on this problems..
Topic archived. No new replies allowed.