注册 登录
编程论坛 C++教室

新手求助!!!!

me100422 发布于 2012-12-13 12:57, 563 次点击
#include<iostream>
using namespace std;
class point
{
public:
    int x;
    int y;
   
    point(int xx,int yy):x(xx),y(yy)
    {}
    void show()
    {
        cout<<x<<" "<<y<<endl;
    }
   
};
class my:public point
{
public:
    my(int xx,int yy,int aa,int bb):x(xx),y(yy),a(aa),b(bb){}
    void get()
    {
        cout<<a<<" "<<b<<endl;
    }
private:
    int a,b;
};
int main()
{
    my m(1,2,3,4);
    m.show();
    m.get();
    return 0;
}
--------------------Configuration: 10 - Win32 Debug--------------------
Compiling...
10.cpp
E:\C++6.0\Microsoft Visual Studio\MyProjects\10\10.cpp(20) : error C2512: 'point' : no appropriate default constructor available
E:\C++6.0\Microsoft Visual Studio\MyProjects\10\10.cpp(20) : error C2614: 'my' : illegal member initialization: 'y' is not a base or member
E:\C++6.0\Microsoft Visual Studio\MyProjects\10\10.cpp(20) : error C2614: 'my' : illegal member initialization: 'x' is not a base or member
执行 cl.exe 时出错.

10.obj - 1 error(s), 0 warning(s)
10 回复
#2
yuccn2012-12-13 13:02
class my:public point
{
public:
    my(int xx,int yy,int aa,int bb): point(xx,yy),a(aa),b(bb){}
    void get()
    {
        cout<<a<<" "<<b<<endl;
    }
private:
    int a,b;
};
要让基类有合适的构造函数才行的
#3
深藏依旧2012-12-13 13:05
point中的x y是私有成员,而你继承point 是public型的  所以在my类中不能用point类中的x y数据成员
#4
深藏依旧2012-12-13 13:07
楼上的初始化对的
#5
me1004222012-12-13 13:07
回复 3楼 深藏依旧
point中的x,y是共有的
#6
深藏依旧2012-12-13 13:12
失误!!
#7
深藏依旧2012-12-13 13:12
习惯定义成私有的了
#8
me1004222012-12-13 13:16
回复 2楼 yuccn
恩恩 成功了 为什么像我那样写不行啊
#9
mmmmmmmmmmmm2012-12-13 13:28
执行父类的构造函数
#10
me1004222012-12-14 15:07
回复 9楼 mmmmmmmmmmmm
这样写 也可以 为什么呢
#include<iostream>
using namespace std;
class point
{
public:
    int x;
    int y;
    point(){}
    point(int xx,int yy)
    {
        x=xx;
        y=yy;
    }
    void show()
    {
        cout<<x<<" "<<y<<endl;
    }
   
};
class my:public point
{
public:
    my(int xx,int yy,int aa,int bb)
    {
        a=aa;
        b=bb;
        x=xx;
        y=yy;
    }
    void get()
    {
        cout<<a<<" "<<b<<endl;
    }
private:
    int a,b;
};
int main()
{
    my m(1,2,3,4);
    m.show();
    m.get();
    return 0;
}
#11
yuccn2012-12-14 16:21
回复 10楼 me100422
主要是基类能够构造就行了
1