/ / पॉइंट की एक सरणी के साथ इनपुट ऑपरेटर को ओवरलोड करना - सी ++, पॉइंटर्स

पॉइंटर्स की एक सरणी के साथ इनपुट ऑपरेटर को ओवरलोड करना - सी ++, पॉइंटर्स

एक वर्ग परियोजना के लिए मेरे पास पॉइंटर्स का 2 डी सरणी है। मैं कंस्ट्रक्टर्स, डिस्ट्रक्टर्स आदि को समझता हूं, लेकिन मुझे यह समझने में समस्याएँ हैं कि ऐरे में वैल्यूज़ कैसे सेट करें। हम वैल्यूज़ इनपुट करने के लिए ओवरलोड इनपुट ऑपरेटर का उपयोग कर रहे हैं। यहाँ उस ऑपरेटर के लिए अब तक का कोड है:

istream& operator>>(istream& input, Matrix& matrix)
{
bool inputCheck = false;
int cols;

while(inputCheck == false)
{
cout << "Input Matrix: Enter # rows and # columns:" << endl;

input >> matrix.mRows >> cols;
matrix.mCols = cols/2;

//checking for invalid input
if(matrix.mRows <= 0 || cols <= 0)
{
cout << "Input was invalid. Try using integers." << endl;
inputCheck = false;
}
else
{
inputCheck = true;
}

input.clear();
input.ignore(80, "n");
}

if(inputCheck = true)
{
cout << "Input the matrix:" << endl;

for(int i=0;i< matrix.mRows;i++)
{
Complex newComplex;
input >> newComplex;
matrix.complexArray[i] = newComplex; //this line
}
}
return input;
}

जाहिर है मेरे पास जो असाइनमेंट स्टेटमेंट है वह हैगलत है, लेकिन मुझे यकीन नहीं है कि यह कैसे काम करना चाहिए। यह मुख्य निर्माणकर्ता जैसा दिखता है:

Matrix::Matrix(int r, int c)
{
if(r>0 && c>0)
{
mRows = r;
mCols = c;
}
else
{
mRows = 0;
mCols = 0;
}

if(mRows < MAX_ROWS && mCols < MAX_COLUMNS)
{
complexArray= new compArrayPtr[mRows];

for(int i=0;i<mRows;i++)
{
complexArray[i] = new Complex[mCols];
}
}
}

और यहाँ मैट्रिक्स है। इसलिए आप विशेषताओं को देख सकते हैं:

class Matrix
{
friend istream& operator>>(istream&, Matrix&);

friend ostream& operator<<(ostream&, const Matrix&);

private:
int mRows;
int mCols;
static const int MAX_ROWS = 10;
static const int MAX_COLUMNS = 15;
//type is a pointer to an int type
typedef Complex* compArrayPtr;
//an array of pointers to int type
compArrayPtr *complexArray;

public:

Matrix(int=0,int=0);
Matrix(Complex&);
~Matrix();
Matrix(Matrix&);

};
#endif

त्रुटि "मी हो रही है" जटिल मैट्रिक्स को मैट्रिक्स में परिवर्तित नहीं कर सकती है :: compArrayPtr (उर्फ कॉम्प्लेक्स *) असाइनमेंट में "यदि कोई समझा सकता है कि मैं क्या गलत कर रहा हूं, तो मैं बहुत आभारी हूं।"

उत्तर:

उत्तर № 1 के लिए 1

तुंहारे newComplex प्रकार की एक वस्तु है Complex (एक मान) और आप इसे असाइन करने का प्रयास करते हैं Complex* सूचक।

इस कार्य के लिए आपको एक जटिल गतिशील रूप से निर्माण करना चाहिए

Complex* newComplex = new Complex();
input >> *newComplex;
matrix.complexArray[i] = newComplex;

लेकिन गतिशील आवंटन (स्मृति प्रबंधन, स्वामित्व, साझा राज्य ...) के साथ आने वाले सभी परिणामों से अवगत रहें।