I'm getting compilation errors relating to my list constructor and I'm not sure what's wrong. In defense of the rest of it, my teacher is probably the worst I've had in any subject ever - no exaggeration - and I'm practically self-taught. I've been trying to learn classes(hence trying to use it) and had a homework problem using sorted lists. I've gotten in a bad habit of trying stuff I don't know will work then spending extra time debugging, as for any dumber errors.
I need to print an array then sort it in ascending order once with a bubble sort and once with a selection sort. Thanks in advance!
Here's the error:
error: request for member âprintâ in âSortâ, which is of non-class type âList()â
#include <iostream>
using namespace std;
const int SIZE = 8;
int array[] = {8, 256, 16, 64, 2, 4, 128, 32};
class List
{
public:
List();
void print()
{
for(int i = 0; i < SIZE; i++)
{
cout << array[i] << ", ";
}
cout << endl;
};
void bubble()
{
for(int i = 0; i < 8; ++i) //pre-increment?
{
for(int j = 0; j < (8 - i); ++j)
{
int swap = array[j];
array[j] = array[j+1];
array[j+1] = swap;
}
}
};
void select()
{
for(int i = 0; i <= SIZE - 1; i++) // might need to make size - 2 bcuz \0
{
int min = i; //minimum value of the array
for(int j = (i + 1); j < SIZE; j++)
{
if(array[j] < array[min])
{
min = j;
}
int swap = array[i];
array[i] = array[min];
array[min] = swap; //not sure of the reason for the redundancy
}
}
};
};
int main()
{
List Sort();
cout << "The unsorted list is: " << Sort.print();
cout << "The list ordered with a selection sort is: " << Sort.print.select();
cout << "The unsorted list again: " << Sort.print();
cout << "The list ordered with a bubble sort is: " << Sort.print.bubble();
void select()
that's a function called select that doesn't return a value int main()
that's a function called main that returns an integer List Sort();
that's a function called Sort that returns a List.
To create an object using the default construtor List Sort;
no parenthesis.
Edit: more errors will appear then. cout << "The unsorted list is: " << Sort.print();
`.print()' doesn't return a value, ¿what do you expect `cout' to do?
Instead
1 2
cout << "The unsorted list is: ";
Sort.print();
cout << "The list ordered with a selection sort is: " << Sort.print.select();
¿what is Sort.print.select() supposed to mean?
don't try to do everything at once
1 2 3
Sort.select();
cout << "The list ordered with a selection sort is: ";
Sort.print();
fix the
1 2
cout << "The unsorted list again: " << Sort.print();
cout << "The list ordered with a bubble sort is: " << Sort.print.bubble();
in a similar way
List();
you need to provide a body for that function, for example List() = default; //equivalent to List(){}
now, a design question. ¿why does your object has no state (no member variables) and none of its member function ask for parameters?