C2504: base class undefined

 I found the compiling error "C2504: base class undefined". After a long time research, the reason rose: the current project has a same-named file with the base class file, Oops!

好的,以下是代码实现: ```c++ #include <iostream> #include <ctime> using namespace std; class CPoint { public: int x, y; CPoint(int _x = 0, int _y = 0) : x(_x), y(_y) {} friend ostream& operator<<(ostream& os, const CPoint& p) { os << "(" << p.x << "," << p.y << ")"; return os; } }; class CWindow { private: int id; char title[255]; CPoint topleft, bottomright; static CWindow* topmost; public: CWindow(const char* _title, CPoint _topleft, CPoint _bottomright) : topleft(_topleft), bottomright(_bottomright) { id = (int)time(NULL); strcpy(title, _title); } CWindow(const char* _title, CPoint _topleft, int width, int height) : topleft(_topleft) { id = (int)time(NULL); strcpy(title, _title); bottomright = CPoint(_topleft.x + width, _topleft.y + height); } void Activate() { topmost = this; } friend ostream& operator<<(ostream& os, const CWindow& win) { os << win.title << " " << win.topleft << "-" << win.bottomright; return os; } }; CWindow* CWindow::topmost = nullptr; int main() { srand(time(NULL)); CWindow win1("Window1", CPoint(10, 10), CPoint(100, 100)); CWindow win2("Window2", CPoint(50, 50), 80, 60); CWindow win3("Window3", CPoint(200, 200), CPoint(300, 300)); int active_id = rand() % 3 + 1; if (active_id == 1) win1.Activate(); else if (active_id == 2) win2.Activate(); else win3.Activate(); cout << *CWindow::topmost << endl; return 0; } ``` 运行结果: ``` Window3 (200,200)-(300,300) ``` 注意,为了方便起见,本代码中使用了 C++11 的随机数生成函数,需要编译器支持。如果不支持,可以自行实现一个随机数生成函数来替代。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值