#include
using namespace std;
class CPU
{
CPU(int c=1);
};
class RAM
{
RAM(int ram=2);
};
class CDROM
{
CDROM(int cdrom=3);
};
class Computer
{
public:
Computer(CPU c,RAM r,CDROM d);
~Computer();
void run()
{
cout<<"cpu为:"<<" "<<"ram为:"<<" "<<"cdrom为:"<<endl;
cout<<"调用的是这个run函数"<<endl;
}
void stop()
{
cout<<"调用的是这个stop函数"<<endl;
}
private:
CPU cpu;
RAM ram;
CDROM cdrom;
};
Computer::Computer(CPU c,RAM r,CDROM d):cpu(c),ram(r),cdrom(d)
{
cout<<"调用的是构造函数"<<endl;
}
Computer::~Computer()
{
cout<<"调用的是该析构函数"<<endl;
}
int main()
{
Computer thing();
thing.run();
thing.stop();
return 0;
}

这串代码运行不起来?报错为:.stop”的左边必须有类/结构/联合,“.run”的左边必须有类/结构/联合
- 写回答
- 好问题 1 提建议
- 关注问题
- 邀请回答
-
1条回答 默认 最新
- threenewbee 2018-12-30 08:48关注
你的类定义不符合语法
#include <iostream> using namespace std; class CPU { public: CPU(int c = 1) {} }; class RAM { public: RAM(int ram = 2) {} }; class CDROM { public: CDROM(int cdrom = 3) {} }; class Computer { public: Computer(CPU c, RAM r, CDROM d); ~Computer(); void run() { cout << "cpu为:" << " " << "ram为:" << " " << "cdrom为:" << endl; cout << "调用的是这个run函数" << endl; } void stop() { cout << "调用的是这个stop函数" << endl; } private: CPU cpu; RAM ram; CDROM cdrom; }; Computer::Computer(CPU c, RAM r, CDROM d) :cpu(c), ram(r), cdrom(d) { cout << "调用的是构造函数" << endl; } Computer::~Computer() { cout << "调用的是该析构函数" << endl; } int main() { CPU c; RAM r; CDROM cd; Computer thing(c, r, cd); thing.run(); thing.stop(); return 0; }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报