Let me summarize the core of this blog:
- In C++, it is not allowed to define the same variable repeatedly, otherwise the value of the initial definition is reserved (although the compilation can pass)
- When it comes to judging multiple branches, variables should be defined before entering the judging branch. If they are defined separately in the branch, a compilation error will occur (the variable definition cannot be found)
Problem 1: Variable override fails
report error
First, I defined three initial matrices G, B, A. I hope that when m=4, "Larger transform matrices" can be printed, and the values (including sizes) of the three matrices will be updated.
int m = 4;
mat G = {
{
1.0, 0.0, 0.0},
{
0.5, 0.5, 0.5},
{
0.5, -0.5, 0.5},
{
0.0, 0.0, 1.0} };
mat B = {
{
1, 0, 0, 0},
{
0, 1, -1, 1},
{
-1, 1, 1, 0},
{
0, 0, 0, -1} };
mat A = {
{
1, 0},
{
1, 1},
{
1, -1},
{
0, -1}};
if (m == 4) {
cout << "Larger transform matrices" << endl;
mat G = {
{
1.0/4, 0.0, 0.0},
{
-1.0/6, -1.0/6, -1.0/6},
{
-1.0/6, 1.0/6, -1.0/6},
{
1.0/24, 1.0/12, 1.0/6},
{
1.0/24, -1.0/12, 1.0/6},
{
0.0, 0.0, 1.0}};
mat B = {
{
4.0, 0.0, 0.0, 0.0, 0.0},
{
0.0, -4.0, 4.0, -2.0, 2.0, 4.0},
{
-5.0, -4.0, -4.0, -1.0, -1.0, 0.0},
{
0.0, 1.0, -1.0, 2.0, -2.0, -5.0},
{
1.0, 1.0, 1.0, 1.0, 1.0, 0.0},
{
0.0, 0.0, 0.0, 0.0, 0.0, 1.0} };
mat A = {
{
1, 0.0, 0.0, 0.0},
{
1.0, 1.0, 1.0, 1.0},
{
1.0, -1.0, 1.0, -1.0},
{
1.0, 2.0, 4.0, 8.0},
{
1.0, -2.0, 4.0, -8.0},
{
0.0, 0.0, 0.0, 1.0}};
}
There is no error in compilation, but when running, "Larger transform matrices" is successfully printed, but an error occurs:
So, continue to debug... It is found that the size of the matrix G in one step is still the originally defined 4x4 matrix, and the 6x3 matrix when m=4 is not assigned, thus causing an overflow error.
solve
The problem is that in the if branch, the three matrices mat A, B, and G are repeatedly defined, and the mat in front of them needs to be deleted and assigned directly.
int m = 4;
mat G = {
{
1.0, 0.0, 0.0},
{
0.5, 0.5, 0.5},
{
0.5, -0.5, 0.5},
{
0.0, 0.0, 1.0} };
mat B = {
{
1, 0, 0, 0},
{
0, 1, -1, 1},
{
-1, 1, 1, 0},
{
0, 0, 0, -1} };
mat A = {
{
1, 0},
{
1, 1},
{
1, -1},
{
0, -1}};
if (m == 4) {
cout << "Larger transform matrices" << endl;
G = {
{
1.0/4, 0.0, 0.0}, // 删掉了前面的mat,直接赋值
{
-1.0/6, -1.0/6, -1.0/6},
{
-1.0/6, 1.0/6, -1.0/6},
{
1.0/24, 1.0/12, 1.0/6},
{
1.0/24, -1.0/12, 1.0/6},
{
0.0, 0.0, 1.0}};
B = {
{
4.0, 0.0, 0.0, 0.0, 0.0}, // 删掉了前面的mat,直接赋值
{
0.0, -4.0, 4.0, -2.0, 2.0, 4.0},
{
-5.0, -4.0, -4.0, -1.0, -1.0, 0.0},
{
0.0, 1.0, -1.0, 2.0, -2.0, -5.0},
{
1.0, 1.0, 1.0, 1.0, 1.0, 0.0},
{
0.0, 0.0, 0.0, 0.0, 0.0, 1.0} };
A = {
{
1, 0.0, 0.0, 0.0}, // 删掉了前面的mat,直接赋值
{
1.0, 1.0, 1.0, 1.0},
{
1.0, -1.0, 1.0, -1.0},
{
1.0, 2.0, 4.0, 8.0},
{
1.0, -2.0, 4.0, -8.0},
{
0.0, 0.0, 0.0, 1.0}};
}
The operation is successful, and we can see that the G matrix is also the value we expected.
Return to the simplest example
#include <iostream>
#include <cstdio>
using namespace std;
int main() {
int m = 2;
int a = 100;
if (m == 2) {
int a = 200; // 这里重复定义了int a,应该直接写成 a = 200 就正确了
}
cout << a << endl;
return 0;
}
Compiling like this is fine, but the output is wrong:
100
Explain that C++ does not allow the same variable to be defined repeatedly, otherwise the value of the variable defined for the first time is retained.
Question 2: The variable in the judgment branch is undefined
report error
Here m either takes 2 or takes 4, so there will be two branches. So, I used the following writing method:
int m = 2;
if (m == 2) {
// 分支1
mat G = {
{
1.0, 0.0, 0.0},
{
0.5, 0.5, 0.5},
{
0.5, -0.5, 0.5},
{
0.0, 0.0, 1.0} };
mat B = {
{
1, 0, 0, 0},
{
0, 1, -1, 1},
{
-1, 1, 1, 0},
{
0, 0, 0, -1} };
mat A = {
{
1, 0},
{
1, 1},
{
1, -1},
{
0, -1}};
}
if (m == 4) {
// 分支2
cout << "Larger transform matrices" << endl;
mat G = {
{
1.0/4, 0.0, 0.0},
{
-1.0/6, -1.0/6, -1.0/6},
{
-1.0/6, 1.0/6, -1.0/6},
{
1.0/24, 1.0/12, 1.0/6},
{
1.0/24, -1.0/12, 1.0/6},
{
0.0, 0.0, 1.0}};
mat B = {
{
4.0, 0.0, 0.0, 0.0, 0.0},
{
0.0, -4.0, 4.0, -2.0, 2.0, 4.0},
{
-5.0, -4.0, -4.0, -1.0, -1.0, 0.0},
{
0.0, 1.0, -1.0, 2.0, -2.0, -5.0},
{
1.0, 1.0, 1.0, 1.0, 1.0, 0.0},
{
0.0, 0.0, 0.0, 0.0, 0.0, 1.0} };
mat A = {
{
1, 0.0, 0.0, 0.0},
{
1.0, 1.0, 1.0, 1.0},
{
1.0, -1.0, 1.0, -1.0},
{
1.0, 2.0, 4.0, 8.0},
{
1.0, -2.0, 4.0, -8.0},
{
0.0, 0.0, 0.0, 1.0}};
}
Then the following variable undefined error occurred during compilation:
solve
Before entering all if branches, define the variable name. In the branch, there is no need to define variables, just assign values directly:
int m = 2;
mat G, B, A; // 进所有if分支之前定义好变量
if (m == 2) {
G = {
{
1.0, 0.0, 0.0}, // 删掉了前面的mat,直接赋值
{
0.5, 0.5, 0.5},
{
0.5, -0.5, 0.5},
{
0.0, 0.0, 1.0} };
B = {
{
1, 0, 0, 0}, // 删掉了前面的mat,直接赋值
{
0, 1, -1, 1},
{
-1, 1, 1, 0},
{
0, 0, 0, -1} };
A = {
{
1, 0}, // 删掉了前面的mat,直接赋值
{
1, 1},
{
1, -1},
{
0, -1}};
}
if (m == 4) {
cout << "Larger transform matrices" << endl;
G = {
{
1.0/4, 0.0, 0.0}, // 删掉了前面的mat,直接赋值
{
-1.0/6, -1.0/6, -1.0/6},
{
-1.0/6, 1.0/6, -1.0/6},
{
1.0/24, 1.0/12, 1.0/6},
{
1.0/24, -1.0/12, 1.0/6},
{
0.0, 0.0, 1.0}};
B = {
{
4.0, 0.0, 0.0, 0.0, 0.0}, // 删掉了前面的mat,直接赋值
{
0.0, -4.0, 4.0, -2.0, 2.0, 4.0},
{
-5.0, -4.0, -4.0, -1.0, -1.0, 0.0},
{
0.0, 1.0, -1.0, 2.0, -2.0, -5.0},
{
1.0, 1.0, 1.0, 1.0, 1.0, 0.0},
{
0.0, 0.0, 0.0, 0.0, 0.0, 1.0} };
A = {
{
1, 0.0, 0.0, 0.0}, // 删掉了前面的mat,直接赋值
{
1.0, 1.0, 1.0, 1.0},
{
1.0, -1.0, 1.0, -1.0},
{
1.0, 2.0, 4.0, 8.0},
{
1.0, -2.0, 4.0, -8.0},
{
0.0, 0.0, 0.0, 1.0}};
}
So, it compiles successfully and runs successfully.
Return to the simplest example
#include <iostream>
#include <cstdio>
using namespace std;
int main() {
int m = 2;
if (m == 1) {
int a = 100;
}
if (m == 2) {
int a = 200;
}
cout << a << endl;
return 0;
}
Write compile error like this:
It should be changed to (defining the variable name before the judgment statement):
#include <iostream>
#include <cstdio>
using namespace std;
int main() {
int m = 2;
int a;
if (m == 1) {
a = 100;
}
if (m == 2) {
a = 200;
}
cout << a << endl;
return 0;
}
So, the compilation passes and the result is correct.
At this point, think about another question. If we repeatedly define a in the judgment branch based on this code, that is, write it in the form of int a = xxx, will the above-mentioned repeated definition error occur?
#include <iostream>
#include <cstdio>
using namespace std;
int main() {
int m = 2;
int a;
if (m == 1) {
int a = 100;
}
if (m == 2) {
int a = 200;
}
cout << a << endl;
return 0;
}
The output error result is:
0
Sure enough, the previous conclusion is confirmed again: C++ does not allow the same variable to be defined repeatedly, otherwise the value defined for the first time will be retained, that is, the default value of a in this example is 0.