【C++】判断语句中对变量的合理定义

先总结一下这篇博客的核心:

  1. C++中,不允许重复定义同一个变量,否则保留的初次定义的值(虽然编译能通过)
  2. 涉及到判断的多分支时,应在进判断分支之前定义好变量,如果在分支中分别定义会出现编译错误(找不到变量定义)

问题 1:变量覆盖失败

报错

首先我定义了三个初始的矩阵G, B, A,希望当m=4时,能打印"Larger transform matrices",并且更新三个矩阵的值(包括大小)。

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}};
}

编译没有出错,运行时,"Larger transform matrices"虽成功打印,但是出现报错:
在这里插入图片描述

于是,继续调试…发现是其中一步矩阵G的大小仍然是最初定义的4x4矩阵,而并没有赋值为m=4时的6x3矩阵,因此导致溢出报错。
在这里插入图片描述

解决

问题在于if分支中,重复定义了mat A, B, G三个矩阵,需要删除掉它们前面的mat,直接赋值即可。

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}};
}

运行成功,并且可以看到G矩阵也是我们期望的值。
在这里插入图片描述

回归最简单的例子

#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;
}

这样写编译没问题,但是输出结果错误:

100

说明C++不允许重复定义同一变量,否则是保留初次定义的变量值。

问题 2:判断分支中的变量未定义

报错

这里的m要么取2要么取4,所以会出现两条分支。于是,我用了如下的写法:

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}};
}

然后在编译时出现了如下变量未定义的报错:
在这里插入图片描述

解决

在进所有的if分支之前,定义好变量名。而在分支里,就不需要再定义变量了,直接赋值即可:

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}};
}

于是,成功编译且成功运行。

回归最简单的例子

#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;
}

这样写编译报错:
在这里插入图片描述

应该改成(在判断语句之前定义好变量名):

#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;
}

于是,编译通过且结果正确。

此时,再思考一个问题,如果我们在这个代码基础上,在判断分支内重复定义a,即写成int a = xxx的形式,是不是会出现前面说的重复定义报错?

#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;
}

输出错误结果为:

0

果然,再次印证了前面的结论:C++不允许重复定义同一变量,否则保留初次定义的值,也就是这个例子中a的默认值0。

猜你喜欢

转载自blog.csdn.net/qq_16763983/article/details/130199285