c++ - 错误: ";" expected before "void"

标签 c++ header

这可能很愚蠢,但我收到一个错误,提示 "; 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/

相关文章:

cookies - 通过 Win Store 应用程序的代码为 webview 设置 cookie/ header

python - 实例化 icmp ipv6 后在 v6 python 套接字中设置 'next header' 字节

c++ - 在 C++ 中初始化动态二维数组

c++ - 确保所有构造函数调用相同的函数 c++,设计模式

c++ - 我的 If Else 语句无法提供 vector 中的最大值 (C++)

c++ - 避免在 std::map/std::unordered_map 中使用 std::string 键查找临时对象

header - MVC3 WebGrid 格式化或样式列标题

c++ - 来自 .evt 文件的 WinAPI LookupAccountSid

header - 使用 Axios 获取请求的 "Content-Disposition"Header

javascript - AngularJs $http 变形请求 url