I need some help on this function definition

I am trying to compile a file personType.cpp and I get this error when compiling operator=
here is the error:
MalikChapter10\Election Results\personType.cpp:35:38: error: invalid initialization of reference of type 'const personType&' from expression of type 'bool'
Process terminated with status 1 (0 minute(s), 0 second(s))
1 error(s), 0 warning(s) (0 minute(s), 0 second(s))

1
2
3
4
5
6
7
8
9
10
  const personType& personType::operator=(const personType& right)
{
	//cout<<"See Programming Exercise 11"<<endl;
	if(this != &right)    //avoid self-copy
        {
return (firstName <= right.firstName
        && lastName <= right.lastName);
        };
	return *this;
}

please help
1
2
return (firstName <= right.firstName
        && lastName <= right.lastName);

This returns a boolean. But what does the operator say it returns?


const personType& personType::operator=(const personType& right)

Is that the same as a bool? No. personType& is NOT a bool, so why is this operator trying to return a bool?

This looks like you created this operator by copy-pasting a different operator, but you forgot to take out the original code from the operator you copied.
I can see that is not the proper code for defining operator=
But what is the proper way of defining this function for comparing firstNames and lastNames?
the operator "=" isn't used to compare things, "==" is. "=" is to assign a variable/object to another of the same type.

If you're returning a bool, either as Repeater said you're not returning the right type, or you meant to use "==" and didn't

Hope this helps
I have found another operator= in another program, it is like this:
1
2
3
4
void operator = (const Distance &D ) {
         feet = D.feet;
         inches = D.inches;
      }


So I changed mine to be like this:
1
2
3
4
5
6
7
const candidateType& candidateType::operator=(const candidateType& right)
{
	//cout<<"See Programming Exercise 11."<<endl;
	firstName = right.firstName;
        lastName  = right.lastName;
}


I am not sure if it is correct.
When I compile I get a warning that says:
Election Results\personType.cpp:34:1: warning: no return statement in function returning non-void [-Wreturn-type]

I am not sure what the return statement should say.

The project compiles OK! but it bombs out when I try to run it
return *this;

Stop blindly copying code from the internet and start understanding it first. Including what I just wrote.

Last edited on
Here is the code for the project:
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
 #ifndef H_personType
#define H_personType

#include <iostream>
#include <string>

using namespace std;


class personType 
{
        //Overload the stream insertion and extraction operators.     
	friend istream& operator>>(istream&, personType&);
    friend ostream& operator<<(ostream&, const personType&);

public:
    const personType& operator=(const personType&);
	//Overload the assignment operator.

    void setName(string first, string last);
	//Function to set firstName and lastName according to 
 	//the parameters.
	//Postcondition: firstName = first; lastName = last

    void getName(string& first, string& last);
	//Function to return firstName and lastName via the 
  	//parameters. 	//Postcondition: first = firstName; last = lastName      
	
	personType(string first = "", string last = ""); 	
	//constructor with parameters 	
	//Set firstName and lastName according to the parameters.
	//Postcondition: firstName = first; lastName = last

	  //Overload the relational operators.
    bool operator==(const personType& right) const;
    bool operator!=(const personType& right) const;
    bool operator<=(const personType& right) const;
    bool operator<(const personType& right) const;
    bool operator>=(const personType& right) const;
    bool operator>(const personType& right) const;

protected:
    string firstName; //variable to store the first name
    string lastName;  //variable to store the last name
};
#endif
   

personTypeImp

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
#include <iostream>
#include <string>
#include "personType.h"

using namespace std;

void personType::setName(string first, string last)
{
	//cout<<"See Programming Exercise 11"<<endl;
	firstName = first;
	lastName = last;
}

void personType::getName(string& first, string& last)
{
	//cout<<"See Programming Exercise 11"<<endl;
	first = firstName;
	last = lastName;
}

     //constructor
personType::personType(string first, string last)
{
	//cout<<"See Programming Exercise 11"<<endl;
	firstName = first;
	lastName = last;
}

const personType& personType::operator=(const personType& right)
{
	//cout<<"See Programming Exercise 11"<<endl;
	if(this != &right)  //avoid self=assignment
    firstName = right.firstName;
    lastName = right.lastName;
	return *this;
}

bool personType::operator==(const personType& right) const
{
	return(firstName == right.firstName
		   && lastName == right.lastName);
}

bool personType::operator!=(const personType& right) const
{
	cout<<"See Programming Exercise 11"<<endl;
	//cout<<"See Programming Exercise 11."<<endl;
	return (firstName != right.firstName
            && lastName != right.lastName);
	return false;
}

bool personType::operator<=(const personType& right) const
{
	cout<<"See Programming Exercise 11"<<endl;
	return (firstName <= right.firstName
            && lastName <= right.lastName);

	return false;
}

bool personType::operator<(const personType& right) const
{
	cout<<"See Programming Exercise 11"<<endl;
	return false;
}

bool personType::operator>=(const personType& right) const
{
	cout<<"See Programming Exercise 11"<<endl;
	return (firstName<= right.firstName
         && lastName <= right.lastName);
	return false;
}

bool personType::operator>(const personType& right) const
{
	cout<<"See Programming Exercise 11"<<endl;
	return (firstName > right.firstName
            && lastName > right.lastName);

	return false;
}

istream& operator>>(istream& is, personType& pName)
{
	is>>pName.firstName>>pName.lastName;

 	return is;
}

ostream& operator<<(ostream& os, const personType&  pName)
{
	 cout<<"See Programming Exercise 11"<<endl;
os <<pName.firstName<<pName.lastName;

 	return os;
}

The candidateType
[code]
#ifndef H_candidateType
#define H_candidateType

#include <string>
#include "personType.h"
using namespace std;
const int noOfRegions = 4;
class candidateType: public personType
{
public:
const candidateType& operator=(const candidateType&);
//Overload the assignment operator for the objects of the
//type candidateType.

const candidateType& operator=(const personType&);
//Overload the assignment operator for the objects so
//that the value of an object of the type personType can
//be assigned to an object of the type candidateType.

void setVotes(int region, int votes);
//Function to set the votes of a candidate for a
//particular region.
//Postcondition: The votes specified by the parameter votes
// are assigned to the region specified by the
// parameter region.

void updateVotesByRegion(int region, int votes);
//Function to update the votes of a candidate for a
//particular region.
//Postcondition: The votes specified by the parameter votes
// are added to the region specified by the
// parameter region.

void calculateTotalVotes();
//Function to calculate the total votes received by a
//candidate.
//Postcondition: The votes received in each region are added.

int getTotalVotes();
//Function to return the total votes received by a
//candidate.
//Postcondition: The total votes received by the candidate
// are returned.

void printData() const;
//Function to output the candidate’s name, the votes
//received in each region, and the total votes received.

candidateType();
//default constructor
//Postcondition: Initialize the votes received in each
// region, and the total votes received, to zero.

//Overload the relational operators.
bool operator==(const candidateType& right) const;
bool operator!=(const candidateType& right) const;
bool operator<=(const candidateType& right) const;
bool operator<(const candidateType& right) const;
bool operator>=(const candidateType& right) const;
bool operator>(const candidateType& right) const;

private:
int votesByRegion[noOfRegions];
int totalVotes;
};

#endif


--------------------Election Results--------------------

Votes
Candidate Name Region1 Region2 Region3 Region4 Total
--------------------- ------- ------- ------- ------- ------

4653430
4653430
4653430
4653430
4653430
4653430
Winner: , Votes Received: 0
Total votes polled:4653430
Process returned 0 (0x0) execution time : 0.066 s
Press any key to continue.
next code
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

candidateTypeImp
[code]
#include <iostream>
#include <string>
#include <iomanip>
#include "candidateType.h"

using namespace std;

void candidateType::setVotes(int region, int votes)
{
	votesByRegion[region - 1] = votes;
}

void candidateType::updateVotesByRegion(int region, int votes)
{
	votesByRegion[region - 1] = votesByRegion[region - 1] + votes;
}

void candidateType::calculateTotalVotes()
{
	int i;

	totalVotes = 0;

	for(i = 0; i < noOfRegions; i++)
		totalVotes += votesByRegion[i];
}

int candidateType::getTotalVotes()
{
	return totalVotes;
}

void candidateType::printData() const
{
	cout<<left
	    <<setw(10)<<firstName<<" "
	    <<setw(10)<<lastName<<" ";

	cout<<right;

	for(int i = 0; i < noOfRegions; i++)
		cout<<setw(7)<<votesByRegion[i]<<"  ";
	cout<<setw(7)<<totalVotes<<endl;
}

candidateType::candidateType()
{
	for(int i = 0; i < noOfRegions; i++)
		votesByRegion[i] = 0;

	totalVotes = 0;
}

bool candidateType::operator==(const candidateType& right) const
{
	return(firstName == right.firstName
		   && lastName == right.lastName);
}

bool candidateType::operator!=(const candidateType& right) const
{
	//cout<<"See Programming Exercise 11."<<endl;
	return (firstName != right.firstName
            && lastName != right.lastName);
	return false;
}

bool candidateType::operator<=(const candidateType& right) const
{
	//cout<<"See Programming Exercise 11."<<endl;
	return (firstName <= right.firstName
            && lastName <= right.lastName);
	return false;
}

bool candidateType::operator<(const candidateType& right) const
{
	//cout<<"See Programming Exercise 11."<<endl;
	return (firstName<= right.firstName
         && lastName <= right.lastName);
	return false;
}

bool candidateType::operator>=(const candidateType& right) const
{
	//cout<<"See Programming Exercise 11."<<endl;
	return (firstName >= right.firstName
          &&  lastName >= right.lastName);
	return false;
}

bool candidateType::operator>(const candidateType& right) const
{
	//cout<<"See Programming Exercise 11."<<endl;
	return (firstName > right.firstName
            && lastName > right.lastName);
	return false;
}

const candidateType& candidateType::operator=(const candidateType& right)
{
	//cout<<"See Programming Exercise 11."<<endl;
        if(this != &right)    //avoid self-copy
        {
        firstName = right.firstName;
        lastName  = right.lastName;
        };

 	return *this;
}

the main program:
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
149
150
151
152
153
154
155
156
157
158
159
160
#include <iostream>
#include <fstream>
#include <string>
#include "personType.h"
#include "candidateType.h"
#include "orderedArrayListType.h"
#include<iomanip>

using namespace std;

const int noOfCandidates = 6;

void fillNames(ifstream& inFile,
			   orderedArrayListType<candidateType>& cList);
void processVotes(ifstream& inFile,
				  orderedArrayListType<candidateType>& cList);
void addVotes(orderedArrayListType<candidateType>& cList);

void printHeading();
void printResults(orderedArrayListType<candidateType>& cList);

int main()
{
	orderedArrayListType<candidateType> candidateList(noOfCandidates);

	candidateType temp;

	ifstream inFile;

	inFile.open("c:\\Martin\\candData.txt");

	fillNames(inFile, candidateList);

	candidateList.selectionSort();


	inFile.close();

	inFile.open("c:\\Martin\\voteData.txt");

	processVotes(inFile,candidateList);

	addVotes(candidateList);

	printHeading();
	printResults(candidateList);

	return 0;
}

void fillNames(ifstream& inFile,
			   orderedArrayListType<candidateType>& cList)
{
	string firstN;
	string lastN;
	int i;

	candidateType temp;

	for(i = 0; i < noOfCandidates; i++)
	{
		inFile>>firstN>>lastN;
		temp.setName(firstN,lastN);
		cList.insertAt(i,temp);
	}
}

void processVotes(ifstream& inFile,
				  orderedArrayListType<candidateType>& cList)
{
	//cout<<"See Programming Exercise 11"<<endl;

        string firstN = " ";
        string lastN = " ";
        int region = 0;
        int votes = 0;
        int candLocation = 0;
        //int votesByRegion = 0;
       // int totalVotes = 0;
      candidateType temp;

     inFile >> firstN >> lastN >> region >> votes;

     temp.setName(firstN, lastN);
     temp.setVotes(region, votes);

     while (inFile)
     {
          candLocation = cList.binarySearch(temp);
            //candidateList.selectionSort();
           if (candLocation != -1)
           {
              cList.retrieveAt(candLocation, temp);
              temp.updateVotesByRegion(region, votes);
              cList.replaceAt(candLocation, temp);
           }

      inFile >> firstN >> lastN >> region >> votes;

      temp.setName(firstN, lastN);
      temp.setVotes(region, votes);
      }
}

void addVotes(orderedArrayListType<candidateType>& cList)
{
	int i;

	candidateType temp;

	for(i = 0; i < noOfCandidates; i++)
	{
		cList.retrieveAt(i,temp);
		temp.calculateTotalVotes();
		cList.replaceAt(i,temp);
	}
}

void printHeading()
{
    cout<<"      --------------------Election Results---------"
		<<"-----------"<<endl<<endl;
    cout<<"                  		         Votes"<<endl;
    cout<<"    Candidate Name     Region1  Region2  Region3  "
 		<<"Region4   Total"<<endl;
    cout<<"---------------------  -------  -------  "
 		<<"-------  -------  ------"<<endl;
}

void printResults(orderedArrayListType<candidateType>& cList)
{
        int sumVotes = 0 , largestVotes = 0, winLoc = 0;
	//cout<<"See Programming Exercise 11"<<endl;
    string firstN, lastN;

    for(int i = 0; i <= noOfCandidates; i++)
    {
        candidateType temp;

        cout << left
          << setw(8) << firstN << " "
          << setw(8) << lastN << " ";

     cout << right;
     for (int i = 0; i < noOfRegions; i++)
        sumVotes = temp.getTotalVotes()  ;
     if (largestVotes < temp.getTotalVotes())
     {
         largestVotes = temp.getTotalVotes();
         winLoc = i;
     }

}
        int getTotalVotes;
        for(int i = 0; i < 6; i++)
            cout << getTotalVotes <<endl;
        cout<<"Winner: "<<firstN<<lastN<<","<<"\t"<< "Votes Received: "<< winLoc<<endl;
        cout <<"Total votes polled:" << getTotalVotes;
}

Here is what I get when I compile and fun the program:

--------------------Election Results--------------------

Votes
Candidate Name Region1 Region2 Region3 Region4 Total
--------------------- ------- ------- ------- ------- ------

4653430
4653430
4653430
4653430
4653430
4653430
Winner: , Votes Received: 0
Total votes polled:4653430
Process returned 0 (0x0) execution time : 0.066 s
Press any key to continue.

These are the data files:
candData.txt

Goldy Goofy
Monty Mickey
Ducky Donald
Peter Pluto
Doctor Doc
Buddy Balto

voteData.txt

Goldy Goofy 2 34
Monty Mickey 1 56
Ducky Donald 2 56
Peter Pluto 1 78
Doctor Doc 4 29
Buddy Balto 4 78
Monty Mickey 2 63
Ducky Donald 1 23
Peter Pluto 2 56
Doctor Doc 1 25
Peter Pluto 4 23
Doctor Doc 4 12
Goldy Goofy 3 134
Buddy Balto 4 82
Monty Mickey 3 67
Ducky Donald 2 67
Doctor Doc 3 67
Buddy Balto 4 23
Monty Mickey 1 56
Ducky Donald 2 35
Peter Pluto 1 27
Doctor Doc 2 34
Goldy Goofy 1 75
Peter Pluto 4 23
Monty Mickey 4 89
Peter Pluto 1 23
Doctor Doc 3 89
Monty Mickey 3 89
Peter Pluto 1 67
Doctor Doc 2 37
Buddy Balto 4 89
Monty Mickey 2 78
Ducky Donald 1 87
Peter Pluto 1 90
Doctor Doc 4 56

The output is completely wrong

please assist
Topic archived. No new replies allowed.