【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。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Mr.zwX

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值