0

Why do I get errors on implementing this program? I'm unable to find anything wrong here.

#include <iostream>

using namespace std;

void exchange(point& p);

int main()
{
    struct point
    {
        int a, b;
    };

    point one = {1,2};

    exchange(one);
    cout << one.a << " , " << one.b;
    return 0;
}

void exchange(point& p)
{
    int temp;
    p.a = temp;
    p.a = p.b;
    p.b = temp;
}

I get errors in another program too, that implements Structures in a similar way.

Sparsh
  • 25
  • 1
  • 4
  • 2
    What errors do you get? – Joey Ciechanowicz Apr 22 '15 at 11:50
  • Not exactly the same, but very similar: [Need help understanding struct, and a few errors with program](http://stackoverflow.com/questions/10809557/need-help-understanding-struct-and-a-few-errors-with-program?rq=1) – crashmstr Apr 22 '15 at 11:54

3 Answers3

6

There no symbol point in the global scope, it's only defined inside the main function and not outside of main.

Define the structure in the global scope, before you use the symbol in the exchange prototype.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
1

You have to declare structure point in global scope to resolve the error that you are getting.

#include <iostream>

using namespace std;
struct point
    {
        int a, b;
    };

void exchange(point& p);

int main()
{


    point one = {1,2};

    exchange(one);
    cout << one.a << " , " << one.b;
    return 0;
}

void exchange(point& p)
{
    int temp=0;
    p.a = temp;
    p.a = p.b;
    p.b = temp;
}
Steephen
  • 14,645
  • 7
  • 40
  • 47
1

The point structure is declared in the scope of main, since your exchange() function is outside of the scope of the main, it throws an error. You need to define your structure outside of main.

Bob
  • 1,779
  • 3
  • 22
  • 35