// a.h static int g_count; void AddCount(int count); // a.c #include “a.h” void AddCount(int count) { g_count += count; } // b.c #include “a.h” void IncrCount(void) { g_count += 1; } void Test(void) { print(“Count init = %d,”, g_count);IncrCount(); print(“incr = %d,”, g_count); AddCount(3); print(“added = %d”, g_count); }
时间: 2024-03-29 07:35:45 浏览: 12
这段代码中存在一个错误。由于在a.h文件中定义的全局变量g_count是static类型的,因此它的作用域仅限于单个.cpp文件,不能在其他文件中访问。因此,在b.c文件中的函数IncrCount和Test都无法访问变量g_count,会导致编译错误。
要解决这个问题,可以将变量g_count声明为extern类型,这样就可以在其他.cpp文件中访问它。修改方式如下:
// a.h
extern int g_count;
void AddCount(int count);
// a.c
#include "a.h"
int g_count;
void AddCount(int count) {
g_count += count;
}
// b.c
#include "a.h"
void IncrCount(void) {
g_count += 1;
}
void Test(void) {
printf("Count init = %d,", g_count);
IncrCount();
printf("incr = %d,", g_count);
AddCount(3);
printf("added = %d", g_count);
}
相关问题
这段代码不显示库存,请进行修改<scroll-view scroll-y> <view wx:if="{{cartList&&cartList.length>0}}"> <view class="list_root"> <block wx:for="{{cartList}}" wx:key="index"> <view class="caipin_item"> <image class="cai_img" src="{{item.icon}}" /> <view class="cai_root2"> <view class="cai_title">{{item.name}}</view> <view class="cai_sell">库存:{{item.num}} </view> <view class="price-stepper"> <view class="cai_price">{{item.price}}</view> <view class="stepper"> <view catchtap="minusCount" data-item="{{item}}"> <image class="jiajian" src="/image/jian.png" /> </view> <view class="value" data-id="{{item._id}}"> {{item.quantity}}</view> <view catchtap="addCount" data-item="{{item}}"> <image class="jiajian" src="/image/jia.png" /> </view> </view> </view> </view> <icon class="shopcart_delete" type="clear" size="18" data-index="{{index}}" data-id="{{item._id}}" catchtap="deleteOne" /> </view> </block> </view>
可以尝试将代码中的 `<view class="cai_sell">库存:{{item.num}} </view>` 修改为 `<view class="cai_sell">库存:{{item.stock}} </view>`,其中 `stock` 为商品的库存属性名,需要根据实际情况进行修改。如果还是不显示库存,可以检查一下数据源中是否有库存属性并且数量正确。
定义学生类Student,成员包含:对象属性:sno、sname、age,类属性count,对象方法show(),用于显示学生信息;类方法addCount(),实现count属性累加1,静态方法showCount(),用于显示count的值。要求所有 属性全部为私有。要求完成以下操作: (1)创建两个学生对象,输出学生基本信息,实现学生对象的累加,输出学生人数。 (2)为第一个学生对象动态添加属性sex(性别)和方法getName()(用于获取学生的姓名),输出学生的姓名和性别。 参考示例:# 类对象zhangsan动态添加对象方法run() zhangsan.run = types.MethodType(run, zhangsan) (需要导入types模块)
下面是实现上述要求的代码:
```python
class Student:
__count = 0
def __init__(self, sno, sname, age):
self.__sno = sno
self.__sname = sname
self.__age = age
Student.__count += 1
def show(self):
print("学号:{},姓名:{},年龄:{}".format(self.__sno, self.__sname, self.__age))
@classmethod
def addCount(cls):
cls.__count += 1
@staticmethod
def showCount():
print("学生人数:{}".format(Student.__count))
# 创建两个学生对象
stu1 = Student("001", "张三", 18)
stu2 = Student("002", "李四", 19)
# 输出学生基本信息,实现学生对象的累加,输出学生人数
stu1.show()
stu2.show()
Student.showCount()
# 为第一个学生对象动态添加属性sex(性别)和方法getName()(用于获取学生的姓名),输出学生的姓名和性别
stu1.__sex = "男"
stu1.getName = lambda self: self._Student__sname
print("{}的性别是{}".format(stu1.getName(), stu1.__sex))
```
输出结果如下:
```
学号:001,姓名:张三,年龄:18
学号:002,姓名:李四,年龄:19
学生人数:2
张三的性别是男
```