0
我有這樣的代碼,我想用,但由於某種原因,我得到上述錯誤進行測試:從「複雜*」到「測試*」的無效轉換[-fpermissive]
#include <iostream>
using namespace std;
class Complex
{
private:
int real;
int imag;
public:
Complex(): real(0), imag(0) { }
void Read()
{
cout<<"Enter real and imaginary number respectively:"<<endl;
cin>>real>>imag;
}
Complex* Add(Complex* comp2)
{
Complex* temp;
temp->real=real+comp2->real;
/* Here, real represents the real data of object c1 because this function is called using code c1.Add(c2) */
temp->imag=imag+comp2->imag;
/* Here, imag represents the imag data of object c1 because this function is called using code c1.Add(c2) */
return temp;
}
void Display()
{
cout<<"Sum="<<real<<"+"<<imag<<"i";
}
};
class Test: public Complex
{
public:
Test() {};
~Test()
{
cout << "\nObject destroyed\n";
};
};
int main()
{
//Complex c1,c2;
Complex* c1 = new Complex();
Complex* c2 = new Complex();
//Test c3;
Test* c3 = new Test();
c1->Read();
c2->Read();
//c3.Read();
c3=c1->Add(c2);
c3->Display();
return 0;
}
可能有人幫助我的錯誤?它來自哪裏?
注意:我正在做一些關於派生類的測試,它可以使用基類中的方法和對象。 我想創建c3 Test-type對象,可以使用使用2複雜類型的對象,添加並通過顯示方法從基類顯示(希望它是有道理的)。
所以爲了實現我的目標,我應該聲明c2和c1是類型Test和c3類型Complex以使用c1和c2調用Add()? – Sabyc90
是的,這是正確的 – Rakete1111