大家帮我看看这个基础的C++程序为啥编译不了?

来源:百度知道 编辑:UC知道 时间:2024/06/18 13:25:14
我主要是想测试一下不同类之间通过PUBLIC声明相互访问变量,可我怎么都编译不了,麻烦大家帮我看看啊~

#include <iostream>
using namespace std;

class Father
{
public:
int x,y;
public:
Father(int a,int b);
int together(int m,int n);
};

Father::Father(int a,int b)
{
x=a;y=b;
}
int Father::together(int m,int n)
{
return m+n;
}

class Child
{
public:
Father f;

public:
void setFather();
Child(Father f);
};
Child::Child(Father f)
{
this->f=f;
cout<<f.x;
}
void Child::setFather()
{

f.x=25;
f.y=77;
cout<<"f.x is "<<f.x<<"f.y is : "<<f.y<<endl;
}

int

Child::Child(Father f)
明显是这里出错了,因为你有定义的Father(int, int),这样,编译器就不会给你生成无参的构造函数,而在你执行Child(Father f),你又用到了无参构造函数,这样,明显会造成编译通不过,有两方法解决:
1.在Father类里添加Father();构造函数。
2.Father::Father(int, int)增加缺省值,如Father(int a=0,int b=0);

class Father
{
public:
int x,y;
public:
Father(int a,int b);
int together(int m,int n);
};
怎么两个public?

它报什么错了,把报的错贴出来,谢谢啦

CHILD的构造函数中this->f=f这=是不合法的吧.除非你对=进行重载