I have a program with all the seven files together and it runs fine

I also have a project with all the files defined there and it gives error messages:
main.cpp:159: undefined reference to 'candidateType::candidateType::getTotalVotes()'
etc.. etc.. etc..
but those files are defined:
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
#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 

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
#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;
}


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
 #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;

	candidateType temp;

	for (int 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;
}

I can post all the other files if needed to like personType.h & personType.cpp. etc
main.cpp:159: undefined reference

Where in the posted code is the line 159 of file main.cpp?
I noticed this, you're declaring temp, candidateType temp;, and never defining it before calling getTotalVotes(), so getTotalVotes() would try to print a data member that was never given a value.

Perhaps you meant to write candidateType temp = cList[i]; but note that when i = noOfCandidates, cList[i]; would be a call to an index out of range.

I don't know if this would cause that particular error message, but just something I noticed. Why would it say "candidateType::candidateType::getTotalVotes()" instead of "candidateType::getTotalVotes()" though?
Last edited on
Topic archived. No new replies allowed.