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;
}
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&'
Your
arrparameter holds an array ofPhoneEntryvalues. You attempt to read an entry from that array and store it in astringwhen you writetemp = arr[j]. You can’t assign aPhoneEntryto astring. Changetempto have a type that can holdPhoneEntryvalues, namelyPhoneEntry:Better yet, use the
swapfunction from the<algorithm>header. Remove thetempvariable and replace the three lines above with this one: