c++ - structure name not declared and swap function swaps without pass by refrence -


i have code,which takes input of 3 students division , b each. 2 divions sorted , merged in 3rd array according birth dates of students. swap function ,i have not passed refrence still swapping , sort output correct !!!.

note:the line below #include..

void swap(struct a,struct b) 

it should

void swap(struct student a,struct student b) 

but without changing program runnong , giving correct outputs !! how ??

#include<iostream> #include<string.h> using namespace std;  void swap(struct a,struct b); void findweek(struct student ar[10],int l,int bd1,int bm1); struct student {     //m month , b birthdate prn=prn number,name=name of student     int m,bd;     char prn[10],name[10]; };  int main() {     //2 divisions , b declared , merged c     struct student a[3], b[3], c[6];      //division input      for(int i=0;i<3;i++)     {         cout<<" enter name of student "<<endl;         cin>>a[i].name;         cout<<"enter prn no. "<<endl;         cin>>a[i].prn;         cout<<"enter birth day "<<endl;         cin>>a[i].bd;         cout<<"enter birth month "<<endl;         cin>>a[i].m;     }      //sorting of      for(int i=0;i<3;i++)     {         for(int j=i;j<3;j++)         {             if(a[i].m>a[j].m)             {                 swap(a[i],a[j]);             }             else if(a[i].m==a[j].m)             {                 if(a[i].bd>a[j].bd)                 {                     swap(a[i],a[j]);                 }             }          }     }       //division b input      for(int i=0;i<3;i++)     {         cout<<" enter name of student "<<endl;         cin>>b[i].name;         cout<<"enter prn no. "<<endl;         cin>>b[i].prn;         cout<<"enter birth day "<<endl;         cin>>b[i].bd;         cout<<"enter birth month "<<endl;         cin>>b[i].m;     }       //sorting of b      for(int i=0;i<3;i++)     {         for(int j=i;j<3;j++)         {             if(b[i].m>b[j].m)             {                 swap(b[i],b[j]);             }             else if(b[i].m==b[j].m)             {                 if(b[i].bd>b[j].bd)                 {                     swap(b[i],b[j]);                 }             }          }     }     cout<<"-----------------------"<<endl;     cout<<"division a"<<endl;     int count=0;     for(int i=0;i<3;i++)     {       //c has merged array , being filled first         c[i]=a[i];         count++;         cout <<c[i].name<<"\t"<<c[i].prn<<"\t"<<c[i].bd<<"|"<<c[i].m<<endl;     }     cout<<"division b"<<endl;     for(int i=0;i<3;i++)     {         //resume filling array count         c[count]=b[i];         cout <<c[count].name<<"\t"<<c[count].prn<<"\t"<<c[count].bd<<"|"<<c[count].m<<endl;         count++;     }      int bd1,bm1;     cout<<"enter date find birthdays in week "<<endl;     cin>>bd1;     cout<<"enter corresponding month "<<endl;     cin>>bm1;     findweek(c,count,bd1,bm1);     return 0; } //to swap structure student arrays sorting void swap(struct student a,struct student b) {     struct student t;     t=a;     a=b;     b=t; }   void findweek(struct student ar[10],int l,int bd1,int bm1) {     int count=0;      for(int i=0;i<l;i++)     {          int month_end=30;         int next_month=bm1+1;         //if(bd1>=23)         int end_date=bd1+7-month_end;     //  else          int endofweek=bd1+7;         //l length of ar , ar=copy of merged array, bd1&bm1 date , month search birthday in week         if((ar[i].m==bm1&&ar[i].bd>=bd1&&ar[i].bd<=endofweek)||ar[i].m==bm1+1&&ar[i].bd<=end_date)         {             if(month_end-bd1>7)                 cout <<ar[i].name<<"\t"<<ar[i].prn<<"\t"<<ar[i].bd<<"|"<<ar[i].m<<endl;             else             {                  if((ar[i].m==bm1&&ar[i].bd>=bd1)||(ar[i].m==next_month&&ar[i].bd<=end_date))                 {                     cout <<ar[i].name<<"\t"<<ar[i].prn<<"\t"<<ar[i].bd<<"|"<<ar[i].m<<endl;                 }              }              count++;             if(count>7)                 break;           }      }  } 

it compiles if remove line:

void swap(struct a,struct b); 

also, compiles if remove whole swap function.
how it?

quite simple.

you defining function takes 2 arguments: incomplete type struct a , incomplete type struct b.
function discarded overload set while searching 1 used @ function call.
main not using swap function. instead, it's using 1 std:: namespace.
introduced iostream or string, it's implementation defined.

try changing name of function or putting throw in implementation of swap. in second case, runtime won't affected.

minimal, (not-)working example reproduce issue:

void f(struct s); struct s {}; int main() { f(s{}); } void f(s) {} 

as can see, error referring incomplete type struct s.

swap kinda of somehow misleading example compiles reasons above.
reproducing issue minimal example helpful.


Comments