标题:[求助]初学者的一个程序错误
只看楼主
没牙的狼
Rank: 1
等 级:新手上路
帖 子:49
专家分:0
注 册:2006-4-23
 问题点数:0 回复次数:6 
[求助]初学者的一个程序错误

#include<iostream.h>

#include<windows.h>
class Base1
{protected:

int number1;
public:
Base1()
{number1=0;
cout<<"number1:"<<number1<<endl;
}
};
class Base2
{protected:
int number2;
public:
Base2(int x)
{
number2=x;

cout<<"number2:"<<number2<<endl;
}
~Base2()
{
cout<<"xi gou:"<<endl;
}
};
class Base3
{protected:

int number3;
public:
Base3(int y)
{
number3=y;

cout<<"number3:"<<number3<<endl;
}
~Base3()
{
cout<<"xi gou:"<<endl;
}
};
class Derive:private Base1,private Base2,private Base3
{private:
Base1 number1base;
Base2 number2base;
Base3 number3base;
public:
Derive(int a,int b);

};
Derive::Derive(int a,int b):number2base(a),number3base(b)
{}

void main()
{
Derive st(4,3);
}
程序目的:三个基类,一个派生类,三个基类有析构,构造函数(一个基类的构造函数无参),派生类有三个私有成员
是三个基类的对象;
从头到尾查了一遍,剩一个错误不知道怎么改:
error C2512: 'Base2' : no appropriate default constructor available
error C2512: 'Base3' : no appropriate default constructor available
请问是什么意思,请高人指点一下..

搜索更多相关主题的帖子: 程序错误 int windows 
2006-04-23 21:44
gototheworld
Rank: 1
等 级:新手上路
帖 子:218
专家分:0
注 册:2006-3-24
得分:0 
以下是引用没牙的狼在2006-4-23 21:44:00的发言:

#include<iostream.h>

#include<windows.h>//去掉,没用
class Base1
{protected:

int number1;
public:
Base1()
{number1=0;
cout<<"number1:"<<number1<<endl;
}
};
class Base2
{protected:
int number2;
public:
Base2(int x)
{
number2=x;

cout<<"number2:"<<number2<<endl;
}
~Base2()
{
cout<<"xi gou:"<<endl;
}
};
class Base3
{protected:

int number3;
public:
Base3(int y)
{
number3=y;

cout<<"number3:"<<number3<<endl;
}
~Base3()
{
cout<<"xi gou:"<<endl;
}
};
class Derive:private Base1,private Base2,private Base3
{/*private:
Base1 number1base;
Base2 number2base;
Base3 number3base;*///不要这样做,没必要再定义对象

public:
Derive(int a,int b);

};
Derive::Derive(int a,int b)::Base1(),Base2(a),Base3(b)
{}

void main()
{
Derive st(4,3);
}

[此贴子已经被作者于2006-4-23 22:31:08编辑过]


路漫漫其修远兮 吾将上下而求索
2006-04-23 22:30
没牙的狼
Rank: 1
等 级:新手上路
帖 子:49
专家分:0
注 册:2006-4-23
得分:0 
楼上的可能没理解我的意思,number1base;

number1base
number2base
number3base
是三个基类的对象,是派生类私有成员,在运行派生类时要设法调用他们......

2006-04-24 06:40
gototheworld
Rank: 1
等 级:新手上路
帖 子:218
专家分:0
注 册:2006-3-24
得分:0 
以下是引用没牙的狼在2006-4-24 6:40:00的发言:
楼上的可能没理解我的意思,number1base;

number1base
number2base
number3base
是三个基类的对象,是派生类私有成员,在运行派生类时要设法调用他们......

你要一开始就说,我就不会搞错了
#include<iostream.h>

#include<windows.h>//去掉,没用
class Base1
{protected:

int number1;
public:
Base1()
{number1=0;
cout<<"number1:"<<number1<<endl;
}
};
class Base2
{protected:
int number2;
public:

Base2(int x = 0)
{
number2=x;

cout<<"number2:"<<number2<<endl;
}
~Base2()
{
cout<<"xi gou:"<<endl;
}
};
class Base3
{protected:

int number3;
public:

Base3(int y = 0)
{
number3=y;

cout<<"number3:"<<number3<<endl;
}
~Base3()
{
cout<<"xi gou:"<<endl;
}
};
class Derive:private Base1,private Base2,private Base3

{private:
Base1 number1base;
Base2 number2base; //
Base3 number3base;//用带默认参数的构造函数
public:
Derive(int a,int b);

};
Derive::Derive(int a,int b):number2base(a),number3base(b)

{


}


void main()
{
Derive st(4,3);
}

[此贴子已经被作者于2006-4-24 10:02:38编辑过]


路漫漫其修远兮 吾将上下而求索
2006-04-24 09:06
没牙的狼
Rank: 1
等 级:新手上路
帖 子:49
专家分:0
注 册:2006-4-23
得分:0 

谢谢gototheworld,调出来了,单我不明白:Base2(int x = 0)
Base3(int y = 0)为什么这么写,主函数中st(4,3)不是已经把x,y
赋值了吗?能在稍微解释一下嘛?


2006-04-24 10:36
gototheworld
Rank: 1
等 级:新手上路
帖 子:218
专家分:0
注 册:2006-3-24
得分:0 
class Derive:private Base1,private Base2,private Base3

{private:
Base1 number1base;
Base2 number2base; //你原来只写了一个构造函数,即
// Base2(int x)
//{
// number2=x;

// cout<<"number2:"<<number2<<endl;
// }但是你这里要调用的是默认构造函数,可是你没写这样的默认构造函数即Base2() {},所以编译出错在这里。
//我是看到你题目的要求“构造函数(一个基类的构造函数无参)”,所以我就没用默认构造函数的写法,
//只是在构造数里加了默认参数,即

Base2(int x = 0)
{
number2=x;

cout<<"number2:"<<number2<<endl;
}这里是用0,去赋值

而主函数中st(4,3),是重新调用构造函数用3,4赋值

Base3 number3base;//最主要的原因是你这里没有构造函数来初始化number3base这个对象,所以就错了,用上面的方法来解决
public:
Derive(int a,int b);

};


路漫漫其修远兮 吾将上下而求索
2006-04-24 11:12
没牙的狼
Rank: 1
等 级:新手上路
帖 子:49
专家分:0
注 册:2006-4-23
得分:0 
感谢gototheworld,说的很明白,我懂了.

2006-04-24 14:28



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-59573-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.329304 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved