C++错误请帮忙看下

来源:百度知道 编辑:UC知道 时间:2024/06/23 07:44:51
#include <iostream>
using namespace std;

enum JB {P1=1,P2,P3,P4,P5,P6,P7};
class CPU
{
private:
JB rank ;
int PL;
float FY;
public:
CPU (JB r, int f, float v)
{
JB rank = r;
PL = f;
FY = v;
cout<<"构造了一个CPU!"<<endl;
}
~CPU () { cout<<"析构了一个CPU!"<<endl; }

JB GetRank() const { return rank; }
int GetPL() const { return PL; }
float GetFY() const { return FY; }

void SetRank(JB r) { rank = r; }
void SetPL(int f) { PL = f; }
void SetFY(float v) { FY = v; }

void Run() {cout<<"CPU开始运行!"<<endl; }
void Stop() {cout<<"CPU停止运行!"<<endl; }
};
class RAM
{
private:
int size;
public:
RAM (int s)
{
size = s;
cout<<"构造了一个RAM!"<<endl;
}

一个C++类通常有一个默认的构造函数,它没有参数。
但是,如果你在类里定义了一个带参数的构造函数,那么这个默认的构造函
数就无效了,除非你还显示定义这个不带参数的构造函数,举例:
class MyClass{
public:
MyClass() //默认构造函数,你可以不定义,缺省就有它
{
}
MyClass( int x ) //另外定义的一个带参数的构造函数
{
}
};

使用时,定义:
MyClass mc;
这条语句,如果上面的类没有定义默认的不带参数的构造函数,那么就会报告你
所说的错误。

自己参照一下,不懂的问我:
#include <iostream>
using namespace std;

enum JB {P1=1,P2,P3,P4,P5,P6,P7};
class CPU
{
private:
JB rank ;
int PL;
float FY;
public:
CPU(){}//add this
CPU (JB r, int f, float v)
{
JB rank = r;
PL = f;
FY = v;
cout<<"构造了一个CPU!"<<endl;
}
~CPU () { cout<<"析构了一个CPU!"<<endl; }

JB GetRank() const { return rank; }
int GetPL() const { return PL; }
float GetFY() const { return FY; }

void SetRank(JB r) { rank = r; }