John Smith
John Smith

Reputation: 55

Sorting Program

I'm having the hardest time with this. I don't even understand the error messages anymore since there's so many of them. I think there's a problem with my alphaGreater() class function parameters but who knows. Can anybody get this to sort alphabetically using the bubble sort inside mySort()?

#include <iostream>
#include <fstream>
#include <string>
#include "phoneEntry.h"
using namespace std;

void mySort(PhoneEntry arr[], int size)
{
    bool inOrder = false;
    string temp;
    for (int i = size - 1; i > 0 && !inOrder; i--)
    {
        inOrder = true;
        for (int j = 0; j < i; j++)
        {
            if(arr[j+1].alphaGreater(arr[j]))
            {
                inOrder = false;
                temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
            }
        }
    }
};

int main()
{   
    const int MAXNUM = 500;
    PhoneEntry entry[MAXNUM];
    ifstream filezilla;
    filezilla.open("phone.txt");
    int count = 0;

    if(filezilla)
    {
        while(count < MAXNUM && entry[count].readEntry(filezilla))
        {
            count++;
            mySort(entry, count);
        }   

        for(int i = 0; i < count; i++)
        {
            entry[i].writeEntry(cout) << endl;
        }
    }
    else
    {
        cout << "404" << endl;
    }

    return 0;
}

Phone Entry Header

Phone Number Header

Sorting Text (http://pastebin.com/HE8Rsmbg)

My Errors...

>> g++ sort.cpp -o PhoneSort.exe
object.cpp: In function 'void mySort(PhoneEntry*, int)':
object.cpp:35:29: error: no match for 'operator=' in 'temp = *(arr + ((unsigned
int)(((unsigned int)j) * 20u)))'
object.cpp:35:29: note: candidates are:
c:\mingw\bin\../lib/gcc/mingw32/4.6.1/include/c++/bits/basic_string.h:541:7: not
e: std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits
, _Alloc>::operator=(const std::basic_string<_CharT, _Traits, _Alloc>&) [with _C
harT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>, st
d::basic_string<_CharT, _Traits, _Alloc> = std::basic_string<char>]
c:\mingw\bin\../lib/gcc/mingw32/4.6.1/include/c++/bits/basic_string.h:541:7: not
e:   no known conversion for argument 1 from 'PhoneEntry' to 'const std::basic_s
tring<char>& {aka const std::basic_string<char>&}'
c:\mingw\bin\../lib/gcc/mingw32/4.6.1/include/c++/bits/basic_string.h:549:7: not
e: std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits
, _Alloc>::operator=(const _CharT*) [with _CharT = char, _Traits = std::char_tra
its<char>, _Alloc = std::allocator<char>, std::basic_string<_CharT, _Traits, _Al
loc> = std::basic_string<char>]
c:\mingw\bin\../lib/gcc/mingw32/4.6.1/include/c++/bits/basic_string.h:549:7: not
e:   no known conversion for argument 1 from 'PhoneEntry' to 'const char*'
c:\mingw\bin\../lib/gcc/mingw32/4.6.1/include/c++/bits/basic_string.h:560:7: not
e: std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits
, _Alloc>::operator=(_CharT) [with _CharT = char, _Traits = std::char_traits<cha
r>, _Alloc = std::allocator<char>, std::basic_string<_CharT, _Traits, _Alloc> =
std::basic_string<char>]
c:\mingw\bin\../lib/gcc/mingw32/4.6.1/include/c++/bits/basic_string.h:560:7: not
e:   no known conversion for argument 1 from 'PhoneEntry' to 'char'
object.cpp:37:30: error: no match for 'operator=' in '*(arr + ((((unsigned int)j
) + 1u) * 20u)) = temp'
object.cpp:37:30: note: candidate is:
phoneEntry.h:12:7: note: PhoneEntry& PhoneEntry::operator=(const PhoneEntry&)
phoneEntry.h:12:7: note:   no known conversion for argument 1 from 'std::string
{aka std::basic_string<char>}' to 'const PhoneEntry&'

Upvotes: 0

Views: 558

Answers (3)

Alan Stokes
Alan Stokes

Reputation: 18964

object.cpp: In function 'void mySort(PhoneEntry*, int)': object.cpp:35:29: error: no match for 'operator=' in 'temp = *(arr + ((unsigned int)(((unsigned int)j) * 20u)))'

So, you're assigning a PhoneEntry to temp. temp is a std::string and PhoneEntry is not - hence the error.

Note that temp is only used in the innermost scope, so you should declare it there, and initialise it on the same line. (In general, you should not declare a local variable without initialising it.)

I would also suggest that you might have a look at the std::swap function.

Upvotes: 0

Rob Kennedy
Rob Kennedy

Reputation: 163267

Your arr parameter holds an array of PhoneEntry values. You attempt to read an entry from that array and store it in a string when you write temp = arr[j]. You can't assign a PhoneEntry to a string. Change temp to have a type that can hold PhoneEntry values, namely PhoneEntry:

PhoneEntry temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;

Better yet, use the swap function from the <algorithm> header. Remove the temp variable and replace the three lines above with this one:

std::swap(arr[j], arr[j + 1]);

Upvotes: 2

Philip Sheard
Philip Sheard

Reputation: 5825

Try

arr[j+1].alphaGreater(arr[j])

instead of

arr.alphaGreater(arr[j])

But I am guessing really.

Upvotes: 0

Related Questions