C2679 error

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
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.
Last edited on
1
2
XVar<float>*pObject = new XVar<float>(x);
	m1.Insert(pObject);


I used a pointer object actually
Line 38 looks suspicious. Is that where the error is?
Last edited on
Yeah it's line 38
In that case you need to dereference the pointer before assigning it to the array element.

 
m_ppXPoints[m_Pos++][m_Pos++] = *pInput;


Another solution you might want to consider is to pass the object by reference.

1
2
void Insert(const XVar<T>& pInput) {
		m_ppXPoints[m_Pos++][m_Pos++] = pInput;

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));).
Last edited on
m_ppXPoints[m_Pos++][m_Pos++] = *pInput;

Wouldn't post-incrementing m_Pos twice in the same statement be undefined behavior?

You need to manage m_pos so you know when and how often it is being incremented.
Topic archived. No new replies allowed.