简体   繁体   中英

error: “;” expected before “void”

This might be stupid but I'm getting an error saying "; expected before "void" in the header file."

#ifndef PA1_H_INCLUDED
#define PA1_H_INCLUDED

void magicSquare(int n);

#endif // PA1_H_INCLUDED

Isn't this the correct way to write the header?

This is the main, where pa1 is the name of the header

 #include <iostream>
 using namespace std
 #include "pa1.h"

int main(){
cout<<"Enter the size of magic square: ";
int n;
cin>>n;                                  //Enter the size of the magic square
if(n%2!=0 && n>=3 && n<=15){             //If the number is odd and between 3 and 15 run the program
int m=n;
magicSquare(m);
}
else{
cout<<"Number is not odd or is out of range."<<endl;   
}
return 0;
}

And this is the magicSquare() function

void magicSquare(int n){
int square[n][n];                     
for (int i=0; i<n; i++){              
for(int j=0; j<n; j++){
    square[i][j]=0;
}
}
int a=0;
int b=n/2;                            
for(int c=1; c<=n*n; c++){            
if(a<0 && b>=n){                  
    a=a+2;
    b--;
}
if (a<0)                          
    a=n-1;
if(b>=n)                          
    b=0;
if(square[a][b]){                 

    a=a+2;
    b--;
}
square[a][b]=c;
 a--, b++;                        
}

 cout<<"Magic square #1 is:"<<endl;
 for (int a=0; a<n; a++){             
 for (int b=0; b<n; b++){
    cout<<square[a][b]<<" ";
}
cout<<endl;
}
}

In the magicsquare() function, I first created the magic square by assigning each value to the correct the position with the various conditions needed to create it and after that I printed the square. I can get it to work only if I write the ; before void in the header

Maybe there is an unrecognized character before this line. Such as unicode white space. Make sure this line is empty.

BTW, if errors in any other header file included before this file will lead to this issue too.

In your case, you have lost ';' after 'using namespace std'.

The mistake is simple; you forgot to add a semicolon before the:

using namespace std;

line. When you add that, you should be fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM