这个C++程序哪错了
//class.h
#include <iostream>
using namespace std;
class c1
{
public:
c1();
c1(int i);
~c1();
private:
int d;
};
c1::c1()
{
cout << "orignal c1 constructor is running...\n";
}
c1::c1(int i)
{
cout << "c1 constructor with parameter is running...\n";
d = i;
}
c1::~c1()
{
cout << "c1 destructor is running...\n";
}
class c2
{
public:
c2();
c2(c2 &t);
~c2();
private:
c1 a;
c1 b;
};
c2::c2() :b(3),a()
{
cout << "c2 constructor is running...\n";
}
c2::c2(c2 &t) :b(t,b),a(t,a)
{
cout << "c2 cloner is running...\n";
}
c2::~c2()
{
cout << "c2 destructor is running...\n";
}
//main.cpp
#include <iostream>
#include "class.h"
using namespace std;
int main()
{
c2 a;
c2 b(a);
return 0;
}
请问哪错了?
答案:就把c2::c2(c2 &t) :b(t,b),a(t,a)
改成c2::c2(c2 &t) :b(t.b),a(t.a)
//class.h
#include <iostream>
using namespace std;
class c1
{
public:
c1();
c1(int i);
~c1();
private:
int d;
};
c1::c1()
{
cout << "orignal c1 constructor is running...\n";
}
c1::c1(int i)
{
cout << "c1 constructor with parameter is running...\n";
d = i;
}
c1::~c1()
{
cout << "c1 destructor is running...\n";
}
class c2
{
public:
c2();
c2(c2 &t);
~c2();
private:
c1 a;
c1 b;
};
c2::c2() :b(3),a()
{
cout << "c2 constructor is running...\n";
}
c2::c2(c2 &t) :b(t.b),a(t.a)
{
cout << "c2 cloner is running...\n";
}
c2::~c2()
{
cout << "c2 destructor is running...\n";
}
//main.cpp
#include <iostream>
#include "class.h"
using namespace std;
int main()
{
c2 a;
c2 b(a);
return 0;
}
你的代码不全,缺少文件class.h!还有就是c2::c2(c2 &t) :b(t,b),a(t,a)有错!
但具体怎么改我不知道。
就把c2::c2(c2 &t) :b(t,b),a(t,a)改成c2::c2(c2 &t) :b(t.b),a(t.a)//class.h#include <iostream>using namespace std;class c1{public:c1();c1(int i);~c1();private:int d;};c1::c1(){cout << "orignal c1 constructor is running...\n";}c1::c1(int i){cout << "c1 constructor with parameter is running...\n";d = i;}c1::~c1(){cout << "c1 destructor is running...\n";}class c2{public:c2();c2(c2 &t);~c2();private:c1 a;c1 b;};c2::c2() :b(3),a(){cout << "c2 constructor is running...\n";}c2::c2(c2 &t) :b(t.b),a(t.a){cout << "c2 cloner is running...\n";}c2::~c2(){cout << "c2 destructor is running...\n";}//main.cpp#include <iostream>#include "class.h"using namespace std;int main(){c2 a;c2 b(a);return 0;
上一个:c++教程的语法是怎样的啊???
下一个:为什么C++教程这么少啊