请教,关于基本c++

来源:百度知道 编辑:UC知道 时间:2024/06/28 23:58:10
//下面程序
//其中为friend和组合类的拷贝构造函数练习
//请问,出错在那里?谢谢!

#include <iostream>
using namespace std;
class A
{
public:
A(int aa=0){a=aa;}
A(A &p);
int Geta(){ return a;}
friend class B;
private:
int a;
};
A::A(A &p)
{
a=p.a;
}
class B
{
public:
B(A aa);
B(B &);
void set(int x);
void display();
private:
A b;
};
B::B(A aa):b(aa)
{
cout<<"B's construction function has been devided!"<<endl;
}
B::B(B &t):b(t.b)
{
cout<<"B's copy c...has....."<<endl;
b=t.b;
}
void B::set(int x)
{
b.a=x;
}
void B::display()
{
cout<<b.a<<endl;
}
int main()
{
A a1;
B b1;
b1.set(3);
b1.display();
return 0;
}

编译时错误

你的问题在这里B b1;
你看你定义的B类:
B(A aa);
B(B &);
你的构造函数都是带参数的,所以 B b1;这样的语句因为找不到B::B()这样的没有参数的构造函数而出错.
而A呢,因为有A(int aa=0){a=aa;} 这个构造函数有默认参数,就可以A a1;这样调用.

下面你是增加B的构造函数还是在main里改写b1的初始化就是你的事了

没有找到 main() 函数

后面三个错误是你函数写的有问题

#include <iostream> 有错
应该是 #include <iostream.h>

LS的
#include <iostream>没有错啊!
这是对的啊