C++关于类的一个小问题,怎么改,急~~~

图片说明#include
using namespace std;
enum CPU_Rank{P1=1,P2,P3,P4,P5,P6,P7};

class CPU{ //CPU类

private:
CPU_Rank rank;
int frequency;
float voltage;

public:
void run();
void stop();

CPU(CPU_Rank a,int b,float c){
    rank=a;
    cout<<"构造成员rank"<<endl;
    frequency=b;
    cout<<"构造成员frequency"<<endl;
    voltage=c;
    cout<<"构造成员voltage"<<endl;
}
CPU();

};

void CPU::run(){
cout<<"成功构造一个CPU对象"<<endl;
}
void CPU::stop(){
cout<<"程序结束"<<endl;
system("pause");
}

class RAM{ //RAM类
public:
RAM(int a,int b,double c){
frequency=a;
size=b;
delay=c;
}
RAM();
private:
int frequency;
int size;
double delay;

};

class CDROM{ //CDROM类
public:
CDROM(int a,int b){
type=a;
speed=b;
}
CDROM();
private:
int type;
int speed;
};

class Computer{ //Computer类
public:
void run();
void stop();
Computer(CPU a,RAM b,CDROM c){
cpu=a;
ram=b;
cdrom=c;
}
private:
CPU cpu;
RAM ram;
CDROM cdrom;
};

void Computer::run(){
cout<<"构造了一个Computer对象"<<endl;
}

void Computer::stop(){
cout<<"停止..."<<endl;
system("pause");
}

int main(int argc,char *argv[]){
CPU cpu(P2,400,5);
RAM ram(1,2,3);
CDROM cdrom(4,7);
Computer computer(cpu,ram,cdrom);
computer.run();
computer.stop();
return 0;
}

c++

1个回答

1.楼主的代码好像头文件没粘贴上来,推测是
2.提示没有找到构造函数的符号,是因为楼主在程序中把默认无参数的构造函数声明了没有给实现。链接器就会报错说找不到实现在哪。修改后参考代码:

#include <iostream>         //没写 
using namespace std;
enum CPU_Rank {P1=1,P2,P3,P4,P5,P6,P7};
class CPU { //CPU类
    private:
        CPU_Rank rank;
        int frequency;
        float voltage;
    public:
        void run();
        void stop();

CPU(CPU_Rank a,int b,float c) {
            rank=a;
            cout<<"构造成员rank"<<endl;
            frequency=b;
            cout<<"构造成员frequency"<<endl;
            voltage=c;
            cout<<"构造成员voltage"<<endl;
        }
        CPU() {     }
        //写出了默认构造函数名,就要给个空的实现
};
void CPU::run() {
    cout<<"成功构造一个CPU对象"<<endl;
}
void CPU::stop() {
    cout<<"程序结束"<<endl;
    system("pause");
}
class RAM { //RAM类
    public:
        RAM(int a,int b,double c) {
            frequency=a;
            size=b;
            delay=c;
        }
        RAM() {     }
        //写出了默认构造函数名,就要给个空的实现 
    private:
        int frequency;
        int size;
        double delay;
};
class CDROM { //CDROM类
    public:
        CDROM(int a,int b) {
            type=a;
            speed=b;
        }
        CDROM() {       }
        //写出了默认构造函数名,就要给个空的实现
    private:
        int type;
        int speed;
};
class Computer { //Computer类
    public:
        void run();
        void stop();
        Computer(CPU a,RAM b,CDROM c) {
            cpu=a;
            ram=b;
            cdrom=c;
        }
    private:
        CPU cpu;
        RAM ram;
        CDROM cdrom;
};
void Computer::run() {
    cout<<"构造了一个Computer对象"<<endl;
}
void Computer::stop() {
    cout<<"停止..."<<endl;
    system("pause");
}
int main(int argc,char *argv[]) {
    CPU cpu(P2,400,5);
    RAM ram(1,2,3);
    CDROM cdrom(4,7);
    Computer computer(cpu,ram,cdrom);
    computer.run();
    computer.stop();
    return 0;
}

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐