学习中遇到的问题:为声明的标识符

本文探讨了C++编程中因类定义顺序不当导致的编译错误,并提供了解决方案。通过调整类之间的声明顺序或提前声明依赖的类,可以避免‘未声明的标识符’这类错误。
#include<iostream>
#include<string>
using namespace std;
**class Building;**
class goodgay {
public:
	goodgay() ;
	void visit();
	Building * building;
};
class Building {
public:
	Building();
	string sittingroom;
private:
	string bedroom;
};
Building::Building() {
	sittingroom = "客厅";
	bedroom = "卧室";
}
goodgay::goodgay() {
	building = new Building;
}
void goodgay::visit() {
	cout << "goodgay正在访问:" << building->sittingroom << endl;
}
void test01() {
	goodgay g;
	g.visit();
}
int main() {
	test01();
}

由于在类goodgay中用了类Building中的对象building,而class Building的定义在class goodgay之后,会报错:“building”未声明的标识符。

解决办法:在class good gay之前添加“class Building;”作为声明,或交换Building和goodgay的定义顺序

出现了这些错误程序3.c(29): error C2143: 语法错误 : 缺少“;”(在“类型”的前面) 1>程序3.c(29): error C2143: 语法错误 : 缺少“;”(在“类型”的前面) 1>程序3.c(29): error C2143: 语法错误 : 缺少“)”(在“类型”的前面) 1>程序3.c(29): error C2143: 语法错误 : 缺少“;”(在“类型”的前面) 1>程序3.c(29): error C2065: “i”:声明标识符 1>程序3.c(29): warning C4552: “<”: 运算符不起任何作用;应输入带副作用的运算符 1>程序3.c(29): error C2065: “i”:声明标识符 1>程序3.c(29): error C2059: 语法错误:“)” 1>程序3.c(29): error C2143: 语法错误 : 缺少“;”(在“{”的前面) 1>程序3.c(30): error C2065: “i”:声明标识符 1>程序3.c(33): error C2065: “i”:声明标识符 1>程序3.c(34): error C2065: “i”:声明标识符 1>程序3.c(34): error C2065: “i”:声明标识符 1>程序3.c(37): error C2065: “i”:声明标识符 1>程序3.c(38): error C2065: “i”:声明标识符 1>程序3.c(38): error C2065: “i”:声明标识符 1>程序3.c(41): error C2065: “i”:声明标识符 1>程序3.c(42): error C2065: “i”:声明标识符 1>程序3.c(42): error C2065: “i”:声明标识符 1>程序3.c(45): error C2065: “i”:声明标识符 1>程序3.c(46): error C2065: “i”:声明标识符 1>程序3.c(46): error C2065: “i”:声明标识符 1>程序3.c(49): error C2065: “i”:声明标识符 1>程序3.c(54): error C2143: 语法错误 : 缺少“;”(在“类型”的前面) 1>程序3.c(54): error C2143: 语法错误 : 缺少“;”(在“类型”的前面) 1>程序3.c(54): error C2143: 语法错误 : 缺少“)”(在“类型”的前面) 1>程序3.c(54): error C2143: 语法错误 : 缺少“;”(在“类型”的前面) 1>程序3.c(54): error C2065: “i”:声明标识符 1>程序3.c(54): warning C4552: “<”: 运算符不起任何作用;应输入带副作用的运算符 1>程序3.c(54): error C2065: “i”:声明标识符 1>程序3.c(54): error C2059: 语法错误:“)” 1>程序3.c(54): error C2143: 语法错误 : 缺少“;”(在“{”的前面) 1>程序3.c(55): error C2065: “i”:声明标识符 1>程序3.c(56): error C2065: “i”:声明标识符 1>程序3.c(57): error C2065: “i”:声明标识符 1>程序3.c(58): error C2065: “i”:声明标识符 1>程序3.c(59): error C2065: “i”:声明标识符 1>程序3.c(60): error C2065: “i”:声明标识符
05-28
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值