C++/plz help

Advertisement


Question
hi
i have a problem about the code below it has an errrore for the push function ,it is"error C2601: 'push' : local function definitions are illegal" can you help me why it has this errore?


#include<stdio.h>
#include<string.h>
#include<iostream.h>

void push(struct Bug position);
void pop(struct Bug position);

int m,n,top=0,q=0;

struct Bug{
  int b;
  int a;
  int x,y;
  int dir;
}move[8],stack[10];

void main(){

  struct Bug position;
  

  int found,mark[40][40],g,h,d=0,i,j,r,p,count=0;
   int   A[]={-1,0,1,1,1,0,-1,-1},B[]={1,1,1,0,-1,-1,-1,0};
   

  printf("Enter m&n:1<m<41,1<n<21\n");
  scanf("%d%d",&m,&n);
  
  for(r=1;r<=m;r++)
     for(p=1;p<=n;p++)
        mark[r][p]=0;

     m=m+1;
     n=n+1;

  r=0;
  for(p=0;p<=n+1;p++)
      mark[r][p]=1;

    r=n+1;
  for(p=0;p<=n+1;p++)
     mark[r][p]=1;
  p=0;
  for(r=0;r<=m+1;r++)
     mark[r][p]=1;
  p=m+1;
  for(r=0;r<=m+1;r++)
     mark[r][p]=1;
       
      
     move[d].a=A[d];
     move[d].b=B[d];
       
     push(position);

  found=1;
  position.dir=0;
  
  

  
   position.x=m/2;
  position.y=n/2;

  
   

  mark[position.x][position.y]=1;

  while(found!=0&&top>=0){
     
     pop(position);
     i=position.x;
     j=position.y;
     d=position.dir;

     while(d<=8&&found!=0){
        printf("******\n");

        g=i+move[d].a;
        h=j+move[d].b;

        if(g==1&&h==1){
         for(q=1;q<=top;q++)
         printf("dir-x=%d,dir-y=%d",stack[q].x,stack[q].y);
         printf("time=%d\n",count);
         found=0;
         }
         else if(mark[g][h]==0){
         

         mark[g][h]=1;
         position.x=i;
         position.y=j;
         position.dir=d;
         push(position);
         i=g;
         j=h;
         d=0;
         }
         else
         d=d+1;
         count++;
     }
}

void push(struct Bug position){/<----------------the push function that has error/
  

  if(q==m*n)
     printf("stack is full\n");

  else{
     
     stack[q]=position;
     top=q;
     
     }
     
  }
  q=q+1;
}
void pop(struct Bug position){

  if(q==0)
     printf("stack is empty\n");
  else{

     position=stack[q];
     q=q-1;
     top=q;
  }
}





thanx
bita

Answer
a brace is wrongly placed. a closing } in main is missing and push has an extra }

void push(struct Bug position){/<----------------the push function that has error/


if(q==m*n)
printf("stack is full\n");

else{

stack[q]=position;
top=q;

}

} // *** here
q=q+1;
}


other errors are:

iostream.h is not part of standard C++. instead, use
#include <iostream>
using namespace std ;

main' must return 'int'

int main(){

      struct Bug position; // position is used uninitialized

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


vijayan

Expertise

my primary areas of interest are generic and template metaprogramming, STL, algorithms, design patterns and c++11. i would not answer questions about gui and web programming.

Experience

about 15 years or so

Education/Credentials
post graduate engineer

©2016 About.com. All rights reserved.