template<class T>
class XVar {
private:
T m_X;
public:
XVar(T x) {
m_X = x;
}
T GetXValue() {
return m_X;
}
};
template<class T>
class Matrix {
protected:
XVar<T> **m_ppXPoints;
int m_Pos;
private:
int m_Size;
public:
Matrix() {}
Matrix(int size) {
m_Size = size;
m_ppXPoints = new T*[m_Size];
for (int i = 0; i < m_Size; i++) {
m_ppXPoints[i] = new T[m_Size];
}
}
~Matrix() {
for (int i = 0; i < m_Size; i++) {
delete[] m_ppXPoints[i];
}
delete m_ppXPoints;
}
void Insert(XVar<T> *pInput) {
m_ppXPoints[m_Pos++][m_Pos++] = pInput;
if (m_Pos > m_Size) {
throw string("Exceeded matrix size");
}
}
};
template<class T>
class MatrixOp :public Matrix<T> {
public:
MatrixOp(int size) {
Matrix(m_Size);
}
void CalcPrint() {
T result;
for (int i = 0; i < m_Pos; i++) {
for (int j = 0; j < m_Pos; j++) {
result = pow(m_ppXPoints[i][j].GetXValue(), 2);
cout << "Given x = " << m_ppXPoints[i][j].GetXValue() << ", Square of " << m_ppXPoints[i][j].GetXValue() << "= " << result;
}
}
}
};
int main() {
int size;
float x;
cout << "Specify matrix size: ";
cin >> size;
MatrixOp<float>m1(size);
cout << "Enter a matrix cell value (or a char to exit): ";
cin >> x;
if (!cin.good()) {
return 0;
}
XVar<float>*pObject = new XVar<float>(x);
m1.Insert(pObject);
while (1) {
cout << "Enter another matrix cell value (or a char tot exit): ";
cin >> x;
if (!cin.good()) {
break;
}
pObject = new XVar<float>(x);
try {
//m1.Insert(pObject);
}
catch (string &out) {
cout << out << endl;
}
}
cout << "Square of matrix cells:" << endl;;
m1.CalcPrint();
delete pObject;
}
Hi guys I am having a problem with my code. I am getting an error which is
Severity Code Description Project File Line Suppression State
Error C2679 binary '=': no operator found which takes a right-hand operand of type 'XVar<T> *' (or there is no acceptable conversion) past year c:\users\user\desktop\c++\past year\past year\past year.cpp 954
I tried to debug it many times but I still can't fix it. The function of the program is to square the element of matrix.
The error message says you are trying to assign a XVar<T>* (a pointer to a XVar<T> object) but there is no such assignment operator. Did your intend to assign a XVar<T> object (not a pointer)? In that case you need to dereferenced the pointer (using the * operator) before assigning it to the variable.
Note that I used a const reference here. It's not strictly necessary but it's makes sense because the object that is being passed to the function is not modified, and it allows you to pass const objects and temporary objects to the function (e.g. m1.Insert(XVar<float>(x));).