1.C++ 全局变量不明确与 using namespace std 冲突
//VS2019
#include<iostream>
using namespace std;
int N;
int count = 0;
void Process(char a, int k, int step)
{
int x, y;
if (k == N * N)
{
if (step > count)
{
count = step;
return;
}
}
...
}
reasons:
std命名空间里有std::count,因此与全局变量count冲突
solutions
- 将全局变量count改为其他名称,如cnt
- 使用count的地方改为::count
- 不要使用using namespace std 为什么我不建议你用【using namespace std】.
2.表达式必须含有常量值
int main()
{
int N = 4;
char a[N][N];
}
reasons:
c++中不允许使用变量作为数组的长度定义数组,必须为常量值,c++中所有的内存需求都是在程序执行前通过定义的常量来确定的。
solutions
动态分配内存
1.一维数组申请
Type *p=new Type[n];
...
delete[] p;
//for example
int num;
cin>>num;
int *a=new int[num];
...
delete[] a;
2.使用vector容器
int num;
cin >> num;
vector a(num);
3.二维数组申请
Type **p=new Type*[m];
//数组p[m][n];
for(int i=0;i<m;++i)
{
p[i]=new Type[n];
}
//for example
while (cin >> N)
{
char** a = new char* [N];
for (int i = 0;i < N;++i)
a[i] = new char[N];
for (int i = 0;i < N;++i)
{
for (int j = 0;j < N;++j)
cin >> a[i][j];
}
}