I\'m not sure why I can\'t get this compile. I\'m trying to test this for a much
ID: 3681927 • Letter: I
Question
I'm not sure why I can't get this compile. I'm trying to test this for a much larger program I'm doing. the compiler says error there's no matching function for call to swap(int&, int) Here is the program: #include
using namespace std;
const int MLS = 50;
typedef int element;
const element SENTINEL = -1;
element read_element();
class Alist {
private:
element items[MLS];
int size;
void Swap();
public:
void Read();
void Print();
void BubbleSort();
};
int main(){
Alist B;
B.Read();
B.Print();
B.BubbleSort();
B.Print();
}
void Alist::Print(){
//PRE: The N.O. Alist is valid.
//POST: The N.O. Alist has had items elements displayed to the user.
for(int i=0; i < size; i++)
B.Read();
B.Print();
B.BubbleSort();
B.Print(); }
void Alist::Print(){
//PRE: The N.O. Alist is valid.
//POST: The N.O. Alist has had items elements displayed to the user
for(int i=0; i < size; i++)
cout << items[i] << endl;
}
void Alist::Read()
{
//PRE: none
//POST: The N.O. Alist is valid, using data from user.
element userval;
size = 0;
cout << "Enter elements," << SENTINEL << " to stop: ";
userval = read_element ();
while((userval != SENTINEL) && (size < MLS)) {
items [size] = userval;
size++;
if (size < MLS) userval = read_element();
else
cout << "List is now full." << endl; }}
void Alist::BubbleSort() {
//PRE: The N.O. Alist is valid.
//POST: The N.O. Alist is unchanged, except its elements have been
//put into ascending order.
for (int i=0; i< size-1; i++)
for (int j=0; j< size-1-i; j++)
if (items[j] > items[j+1]) swap (j,j+1);
else ;}
void Alist::Swap(int pos1, int pos2) {
//PRE: The N.O. Alist is valid.
//POST: Must be >= 0 and size of pos2
//Must be >=0 and < size element temp;
temp = items[pos1];
items[pos1] = items[pos2];
items[pos2] = temp; }
Explanation / Answer
the edited code is here which is compiled,u defined two times the print function and minor errors are fixed
#include<iostream>
#include<stdlib.h>
using namespace std;
const int MLS = 50;
typedef int element;
const element SENTINEL = -1;
element read_element();
class Alist {
private:
element items[MLS];
int size;
void Swap(int,int);
public:
void Read();
void Print();
void BubbleSort();
};
Alist B;
int main(){
B.Read();
B.Print();
B.BubbleSort();
B.Print();
}
void Alist::Print(){
//PRE: The N.O. Alist is valid.
//POST: The N.O. Alist has had items elements displayed to the user.
for(int i=0; i < size; i++)
B.Read();
B.Print();
B.BubbleSort();
for(int i=0; i < size; i++)
cout << items[i] << endl;
}
//void Alist::Print(){
//PRE: The N.O. Alist is valid.
//POST: The N.O. Alist has had items elements displayed to the user
//for(int i=0; i < size; i++)
//cout << items[i] << endl;
//}
void Alist::Read()
{
//PRE: none
//POST: The N.O. Alist is valid, using data from user.
element userval;
size = 0;
cout << "Enter elements," << SENTINEL << " to stop: ";
userval = read_element ();
while((userval != SENTINEL) && (size < MLS)) {
items [size] = userval;
size++;
if (size < MLS) userval = read_element();
else
cout << "List is now full." << endl; }}
void Alist::BubbleSort() {
//PRE: The N.O. Alist is valid.
//POST: The N.O. Alist is unchanged, except its elements have been
//put into ascending order.
for (int i=0; i< size-1; i++)
{
for (int j=0; j< size-1-i; j++)
if (items[j] > items[j+1]) Swap(j,j+1);
}
}
void Alist::Swap(int pos1, int pos2) {
int temp;
//PRE: The N.O. Alist is valid.
//POST: Must be >= 0 and size of pos2
//Must be >=0 and < size element temp;
temp = items[pos1];
items[pos1] = items[pos2];
items[pos2] = temp; }
int read_element()
{
int t;
cin>>t;
return t;
}