Tough function pointer error with assignment operator

Using a bubble sort function and having trouble with my 'Element temp' line.
I get an error for the assignment operator (lines 14 and 16) that says Error: No operator '=' matches these operands. I also tried the //commented line below 'Element temp' and can't get is that way either.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void bubbleSort_Name(Element *ChemicalElements, int size, string name)
{
	Element temp;	
	//Element *temp = new Element();
	bool swap;

	do
	{
		swap = false;
		for (int count = 0; count < (size -1); count++)
		{
			if (ChemicalElements[count].name > ChemicalElements[count + 1].name)
			{
				temp = ChemicalElements[count];
				ChemicalElements[count] = ChemicalElements[count + 1];
				ChemicalElements[count + 1] = temp;
				swap = true;
			}
		}
	}while (swap);
} //end BubbleSort 
Last edited on
Only way I can see this happening is if the 'Element' type is non-copyable, which could happen if you made the = operator private.. or if it has any non-copyable members.

Or it might also happen if its = operator takes the wrong kind of argument.

Does the Element class overload the = operator? If so can you post its prototype?
Took out three of the overloaded functions and a few other items from the end but the rest is here. Class project that is driving me crazy and almost finished.

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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include <string>
#include <ctype.h>
#include <iostream>
#include <iomanip>

using namespace std;

struct Element
{
public:
	double atomicWeight;
	string name;
	string symbol;
	int atomicNumber;

	Element()
	{
		atomicWeight = 0.0; 
		name = " "; 
		symbol = " "; 
		atomicNumber = 0;
	};

	Element(double Weight, string Name, string Symbol, int AtomicNum)
	{
		atomicWeight = Weight;
		name		 = Name;
		symbol		 = Symbol;
		atomicNumber = AtomicNum;
	}
};

//Prototypes
void DisplayElements(Element *ChemicalElements, int size);
void bubbleSort_Name(Element *ChemicalElements, int size, string name);
void bubbleSort_Symbol(string name, Element *ChemicalElements, int size);
void bubbleSort_AtomicWeight(Element *ChemicalElements, int size, double atomicweight);
void bubbleSort_Number(Element *ChemicalElements, int size, int  AtominNum);
void ClearScreen();

int main()
{
	int select = 0;
	char doAgain;

	const int SIZE = 10;
	//Pointer to Element
	Element ChemicalElements;	
	Element *pChemicalElements = new Element[10];

	pChemicalElements = &ChemicalElements;

	pChemicalElements->atomicNumber = 1;
	pChemicalElements->name = "Hydrogen";
	pChemicalElements->atomicWeight = 1.00790;
	pChemicalElements->symbol = "H";
	pChemicalElements++;
//took out other 9 that are initialized here
	


	pChemicalElements = &ChemicalElements;

	do
	{
		do		
		{
			ClearScreen(); //clear from 'Y' || 'y'

			//atomic weight, name, symbol, and atomic number
			cout << "How would you like data sorted?" << endl
				<< "1. Atomic weight " << endl
				<< "2. Name" << endl
				<< "3. Symbol " << endl
				<< "4. Atomic Number " << endl
				<< endl;

			cin >> select;
			ClearScreen();

		}while (select < 1 || select > 4);

		ClearScreen();

		if (select == 1)
			bubbleSort_AtomicWeight(&ChemicalElements, SIZE, 0.0);	
		else if (select == 2)
			bubbleSort_Name(&ChemicalElements, SIZE, " ");	
		else if (select == 3)
			bubbleSort_Symbol(" ", &ChemicalElements, SIZE);	
		else if (select == 4)
			bubbleSort_Number(&ChemicalElements, SIZE, 0);	

		cout << "\nOriginal Element List: \n"
			<< "Weight  Name        Sym Num \n"
			<< "___________________________\n";
		DisplayElements(&ChemicalElements, SIZE);

		cout << "\nSorted Element List: \n"
			<< "Weight  Name        Sym Num \n"
			<< "___________________________\n";
		DisplayElements(&ChemicalElements, SIZE);

		cout << "\nSort another? (Y/N) ";

		cin >> doAgain;

	} while (doAgain == 'Y' || doAgain == 'y');

	return 0;
};

void DisplayElements(Element *ChemicalElements, int size)
{
	for (int index = 0; index < size; index++)
	{
		cout << setw(8) << left << ChemicalElements[index].atomicWeight
			<< setw(12)<< left << ChemicalElements[index].name
			<< setw(4) << left << ChemicalElements[index].symbol 
			<< setw(4) << left << ChemicalElements[index].atomicNumber
			<< endl;
	}
}

void bubbleSort_Name(Element *ChemicalElements, int size, string name)
{
	Element *temp = new Element;	
	//Element *temp = new Element;
	bool swap;

	do
	{
		swap = false;
		for (int count = 0; count < (size -1); count++)
		{
			if (ChemicalElements[count].name > ChemicalElements[count + 1].name)
			{
				temp = ChemicalElements[count];
				ChemicalElements[count] = ChemicalElements[count + 1];
				ChemicalElements[count + 1] = temp;
				swap = true;
			}
		}
	}while (swap);
} //end BubbleSort


After correcting line 127 to be Element temp;, the code compiles fine here (save for some linker errors due to the functions you removed).


Can you post the exact error you're getting?
Professor said this about the error:
"You can't assign anything other than a primitive data type (and string using the string library) with the = statement."

So I changed the function to the below. Now I get a gpf.

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
void bubbleSort_Name(Element *ChemicalElements, int size, string name)
{
	Element temp;	
	Element *ptemp = new Element[1];
	ptemp = &temp;
	bool swap;

	do
	{
		swap = false;
		for (int count = 0; count < (size -1); count++)
		{
			if (ChemicalElements[count].name > ChemicalElements[count + 1].name)
			{
				ptemp = &temp;
				ptemp->atomicNumber = ChemicalElements[count].atomicNumber;
				ptemp->atomicWeight = ChemicalElements[count].atomicWeight;
				ptemp->name = ChemicalElements[count].name;
				ptemp->symbol = ChemicalElements[count].symbol;

				ChemicalElements[count] = ChemicalElements[count + 1];

				ChemicalElements[count].atomicNumber = ptemp->atomicNumber;
				ChemicalElements[count].atomicWeight = ptemp->atomicWeight;
				ChemicalElements[count].name = ptemp->name;
				ChemicalElements[count].symbol = ptemp->symbol;

				//ChemicalElements[count + 1] = ptemp;

				swap = true;
			}
		}
	}while (swap);
} //end BubbleSort 
Professor said this about the error:
"You can't assign anything other than a primitive data type (and string using the string library) with the = statement."


Your professor is mistaken. You can assign any data type with the = operator. The only exception is if you explicitly remove the = operator to prevent copying, which you are not doing here.

EDIT2: Actually, arrays cannot be directly assigned either, so that's another exception /EDIT2

Again... it would really help to see the actual error message you are getting. Because the code you posted before compiles just fine for me.

Also can you post what compiler you are using?

1
2
3
	Element temp;	
	Element *ptemp = new Element[1];
	ptemp = &temp;


This does nothing benefitial. All it does is leak memory (bad). If you're going to use 'temp', just use temp. No need to make a pointer to it, just use it directly.


EDIT: worded less like an asshole.
Last edited on
Topic archived. No new replies allowed.