这可能很愚蠢,但我收到一个错误,提示 "; Expected before the "void"in the header file."
#ifndef PA1_H_INCLUDED
#define PA1_H_INCLUDED
void magicSquare(int n);
#endif // PA1_H_INCLUDED
这不是正确的标题编写方式吗?
这是主体,其中 pa1
是 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;
}
这是magicSquare()
函数
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;
}
}
在 magicsquare()
函数中,我首先通过将每个值分配给正确的位置以及创建它所需的各种条件来创建幻方,然后打印该方形。
只有在标题中的 void
之前写入 ;
才能使其正常工作
最佳答案
这个错误很简单;您忘记在前面添加分号:
using namespace std;
线。当你添加它时,你应该没问题。
关于c++ - 错误: ";" expected before "void",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42195187/