sorting algorithms

Hello, I have been scratching my head over this problem for over a week now. I am teaching myself programing, and the book is not making sense anymore. any help would be apprecaited. thxs!

main.cpp
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
#include <cstdlib>
#include <iostream>
#include <string>
#include <fstream>
#include "candidateType.h"
#include "unorderedArrayListType.h"

using namespace std;

const int NO_OF_CANDIDATES = 6;

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

void printHeading();
void printResults(const unorderedArrayListType<candidateType>& cList);

int main()
{
    unorderedArrayListType<candidateType>candidateList(NO_OF_CANDIDATES);
    
    candidateType temp;
    
    ifstream inFile;
    
    inFile.open("candData.txt");
    if (!inFile)
    {
       cout << "Input file (candData.txt) does not exist. "
            << "Program terminates!!" << endl;
            
       return 1;
}

processVotes(inFile, candidateList);

addVotes(candidateList);

printHeading();
printResults(candidateList);

system("PAUSE");
return 0;
}

void fillNames(ifstream& inFile, unorderedArrayListType<candidateType>& cList)
{
     string firstN;
     string lastN;
     int i;
     candidateType temp;
     
     for (i = 0; i < NO_OF_CANDIDATES; i++)
     {
         inFile >> firstN >> lastN;
         temp.setName(firstN, lastN);
         cList.insertEnd(temp);
     }
}

void processVotes(ifstream& inFile, unorderedArrayListType<candidateType>& cList)
{
     string firstN;
     string lastN;
     int region;
     int votes;
     int candLocation;
     
     candidateType temp;
     
     inFile >> firstN >> lastN >> region >> votes;
     
     temp.setName(firstN, lastN);
     temp.setVotes(region, votes);
     
     while (inFile)
     {
           candLocation = cList.binSearch(temp);
           
           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(unorderedArrayListType<candidateType>& cList)
{
     int i;
     
     candidateType temp;
     
     for (i = 0; i < NO_OF_CANDIDATES; i++)
     {
         cList.retrieveAt(i, temp);
         temp.calculateTotalVotes();
         cList.replaceAt(i, temp);
     }
}

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

arrayListType.h
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
#ifndef H_arrayListType
#define H_arrayListType

template <class elemType>
class arrayListType
{
public:
       bool isEmpty() const;
       
       bool isFull() const;
       
       int listSize() const;
       
       int maxListSize() const;
       
       void print() const;
       
       bool isItemAtEqual(int location, int item) const;
       
       virtual void insertAt(int location, int insertItem) = 0;
       
       virtual void insertEnd(int insertItem) = 0;
       
       void removeAt(int location);
       
       void RetreiveAt(int location, int& retItem) const;
       
       virtual void replaceAt(int location, int repItem) = 0;
       
       void clearList();
       
       virtual int seqSearch(int searchItem) const = 0;
       
       virtual void remove(int removeItem) =0;
       
       arrayListType(int size = 100);
       
       arrayListType (const arrayListType& otherList);
       
       virtual ~arrayListType();
       
protected:
          int *list;
          int length;
          int maxSize;

};

#endif 

arraListType.cpp
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
#include "arrayListType.h"

bool arrayListType::isEmpty() const
{
     return (length == 0);
}

bool arrayListType::isFull() const
{
     return (length == maxSize);
}

int arrayListType::listSize() const
{
    return length;
}

int arrayListType::maxListSize() const
{
    return maxSize;
}

void arrayListType::print() const
{
    for (int i = 0; i < length; i++)
        cout << list[i] << " ";
    cout << endl;
}

bool arrayListType::isItemAtEqual(int location, int item) const
{
     if (location < 0 || location >= length)
     {
        cout << "The location of the item to be removed "
             << "is out of range." << endl;
        
        return false;
     }
else
    return (list[location] == item);
}

void arrayListType::removeAt(int location)
{
     if (location < 0 || location >= length)
        cout << "The location of the item to be removed "
             << "is out of range." << endl;
     
     else
     {
         for (int i = location; i < length - 1; i++)
             list[i] = list[i+1];
         length--;
     }
}

void arrayListType::RetreiveAt(int location, int& retItem) const
{
     if (location < 0 || location >= length)
        cout << "The location of the item to be retreived is "
             << "out of range" << endl;
     else
        retItem = list[location];
}

void arrayListType::clearList()
{
     length = 0;
}

arrayListType::arrayListType(int size)      
{
     if (size <= 0)
     {
        cout << "The array size must be positive. Creating "
             << "an array of the size 100." << endl;
             
        maxSize = 100;
     }
     else
        maxSize = size;
        
     length = 0;
     
     list = new int[maxSize];
}

arrayListType::~arrayListType()
{
     delete [] list;
}

arrayListType::arrayListType(const arrayListType& otherList)
{
     maxSize = otherList.maxSize;
     length = otherList.length;
     
     list = new int[maxSize];
     
     for (int j = 0; j < length; j++)
         list [j] = otherList.list[j];
}
    

candidateType.h
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
#ifndef H_candidateType
#define H_candidateType

#include <string>
#include "personType.h"

const int NO_OF_REGIONS = 4;

class candidateType: public personType
{
public:
      const candidateType& operator=(const candidateType&);
      
      const candidateType& operator=(const personType&);
      
      void updateVotesByRegion(int region, int votes);
      
      void setVotes(int region, int votes);
      
      void calculateTotalVotes();
      
      int getTotalVotes() const;
      
      void printData() const;
      
      candidateType();
      
       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[NO_OF_REGIONS];
        
        int totalVotes;
};
#endif 

candidateType.cpp
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
#include "candidateType.h"

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 < NO_OF_REGIONS; i++)
          totalVotes += votesByRegion[i];
}

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

void candidateType::printData() const
{
     cout << left
          << setw(8) << firstName << " "
          << setw(8) << lastName << " ";
     
     cout << right;
     for (int i = 0; i < NO_OF_REGIONS; i++)
          cout << setw(8) << votesByRegion[i] << " ";
     cout << setw(7) <, totalVotes << endl;
}

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

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

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

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

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

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

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

const candidateType& candidateType::operator=(const candidateType& right)
{
      if (this != &right)
      {
               firstName = right.firstName;
               lastName = right.lastName;
               
               for (int i = 0; i < NO_OF_REGIONS; i++)
                    votesByRegion[i] = right.votesByRegion[i];
               
               totalVotes = right.totalVotes;
      }
      
      return *this;
}         

const candidateType& candidateType::operator=(const personType& right)
{
      firstName = right.getFirstName();
      lastName = right.getLastName();
      
      return *this;
}

personType.h
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
#ifndef H_personType
#define H_personType

#include <string>

using namespace std;

class personType
{
      friend istream& operator>>(istream&, personType&);
      friend ostream& operator<<(ostream&, const personType&);
      
public:
       void setName(string first, string  last);
       
       string getFirstName() const;
       
       string getLastName() const;
       
       personType(string first = "", string last = "");
       
       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;
          string lastName;
};
#endif 

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


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

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

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

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

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

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

istream& operator>>(istream& isObject, personType& pName)
{
         isObject >> pName.firstName >> pName.lastName;
         
         return isObject;
}

ostream& operator<<(ostream& osObject, const personType& pName)
{
         osObject << pName.firstName << " " << pName.lastName;
         
         return osObject;
}

unorderedArrayListType.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#ifndef H_unorderedArrayListType
#define H_unorderedArrayListType

template <class elemType>
class unorderedArrayListType: public arrayListType<elemType>
{
public:
       void insertAt(int location, const elemType& insertItem);
       void insertEnd(const elemType& insertitem);
       void replaceAt(int location, const elemType& repitem);
       void retrieveAt(int location, const elemType& repitem);
       int seqSearch(const elemType& searchItem) const;
       void remove(const elemType& removeItem);
       
       void sort();
       int binSearch(const elemType& item) const;
       
       unorderedArrayListType(int size = 100);
};
#endif 

unorderedArrayListType.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
#include "unorderedArrayListType.h"

template <class elemType>
int unorderedArrayListType<elemType>::binSearch(const elemType& item) const
{
    return binarySearch(list, length, item);
}

template <class elemType>
void unorderedArrayListType<elemType>::sort()
{
     selectionSort(list, length);
}


and my errors:

personType.cpp: In function `std::ostream& operator<<(std::ostream&, const personType&)':
personType.cpp:47: error: no match for 'operator<<' in 'std::operator<< [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>](((std::basic_ostream<char, std::char_traits<char> >&)(+osObject)), ((const std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)((const std::string*)pName))) << " "'
personType.cpp:46: note: candidates are: std::ostream& operator<<(std::ostream&, const personType&)

make.exe: *** [personType.o] Error 1

Execution terminated
Last edited on
Try with the following code change

personType.cpp

1
2
3
4
5
6
ostream& operator<<(ostream& osObject, const personType& pName)
{
         osObject << pName.firstName.c_str() << " " << pName.lastName.c_str();
         
         return osObject;
}
tldr
Missing #include <iostream> in personType.cpp
Also, don't using namespace std; in headers. It pollutes everything that include them.

Use a descriptive title next time.
I made the following adjustments (thxs NVTKrishna, and ne555) but now everything is undeclared!

errors:

In file included from personType.cpp:2:
personType.h:8: error: ISO C++ forbids declaration of `istream' with no type
personType.h:8: error: `istream' is neither function nor member function; cannot be declared friend
personType.h:8: error: expected `;' before '&' token
personType.h:9: error: ISO C++ forbids declaration of `ostream' with no type

personType.h:9: error: `ostream' is neither function nor member function; cannot be declared friend
personType.h:9: error: expected `;' before '&' token
personType.h:12: error: variable or field `setName' declared void
personType.h:12: error: expected `;' before '(' token
personType.h:14: error: `string' does not name a type
personType.h:16: error: `string' does not name a type
personType.h:18: error: expected `)' before "first"
personType.h:28: error: `string' does not name a type
personType.h:29: error: `string' does not name a type
personType.cpp: In member function `bool personType::operator==(const personType&) const':
personType.cpp:7: error: `firstName' undeclared (first use this function)
personType.cpp:7: error: (Each undeclared identifier is reported only once for each function it appears in.)
personType.cpp:7: error: 'const class personType' has no member named 'firstName'
personType.cpp:7: error: `lastName' undeclared (first use this function)
personType.cpp:7: error: 'const class personType' has no member named 'lastName'
personType.cpp: In member function `bool personType::operator!=(const personType&) const':
personType.cpp:12: error: `firstName' undeclared (first use this function)
personType.cpp:12: error: 'const class personType' has no member named 'firstName'
personType.cpp:12: error: `lastName' undeclared (first use this function)
personType.cpp:12: error: 'const class personType' has no member named 'lastName'
personType.cpp: In member function `bool personType::operator<=(const personType&) const':

personType.cpp:17: error: `lastName' undeclared (first use this function)
personType.cpp:17: error: 'const class personType' has no member named 'lastName'
personType.cpp:17: error: 'const class personType' has no member named 'lastName'
personType.cpp:18: error: `firstName' undeclared (first use this function)
personType.cpp:18: error: 'const class personType' has no member named 'firstName'
personType.cpp: In member function `bool personType::operator<(const personType&) const':
personType.cpp:23: error: `lastName' undeclared (first use this function)
personType.cpp:23: error: 'const class personType' has no member named 'lastName'
personType.cpp:23: error: 'const class personType' has no member named 'lastName'
personType.cpp:24: error: `firstName' undeclared (first use this function)
personType.cpp:24: error: 'const class personType' has no member named 'firstName'
personType.cpp: In member function `bool personType::operator>=(const personType&) const':
personType.cpp:29: error: `lastName' undeclared (first use this function)
personType.cpp:29: error: 'const class personType' has no member named 'lastName'
personType.cpp:29: error: 'const class personType' has no member named 'lastName'
personType.cpp:30: error: `firstName' undeclared (first use this function)
personType.cpp:30: error: 'const class personType' has no member named 'firstName'
personType.cpp: In member function `bool personType::operator>(const personType&) const':
personType.cpp:35: error: `lastName' undeclared (first use this function)

personType.cpp:35: error: 'const class personType' has no member named 'lastName'
personType.cpp:35: error: 'const class personType' has no member named 'lastName'
personType.cpp:36: error: `firstName' undeclared (first use this function)
personType.cpp:36: error: 'const class personType' has no member named 'firstName'
personType.cpp: At global scope:
personType.cpp:39: error: expected constructor, destructor, or type conversion before '&' token
personType.cpp:39: error: expected `,' or `;' before '&' token
personType.cpp:46: error: expected constructor, destructor, or type conversion before '&' token
personType.cpp:46: error: expected `,' or `;' before '&' token

make.exe: *** [personType.o] Error 1

Execution terminated
#include <iostream> in personType.h
And you to prefix with std:: the things that are in the std namespace.
1
2
3
friend std::istream& operator>>(std::istream&, personType&);

std::string lastName;
Topic archived. No new replies allowed.