-
April 23rd, 2016, 09:53 AM
#1
Parameters are not being accepted for my printArray function
When I try to call printArray with data it will not let me compile or run my program I don't know what else would go in the parameters to allow the program to work. Anything helps thanks.
Header:
Code:
#pragma once
/*
Header file for the priority queue class
*/
#ifndef PRIORITYQUEUE_H
#define PRIORITYQUEUE_H
class priorityQueue
{
private:
int size;
int *data;
public:
static const int CAPACITY = 50;
priorityQueue();//constructor
~priorityQueue();//destructor
int getParent(int index);
int getLeftChild(int index);
int getRightChild(int index);
void swap(int &, int &);
void insert(int item); //enqueue - heap_insert
void printArray(int []);
void heapify(int index);
//remove and return the smallest item currently in the priority queue
int extractMin();//dequeue
bool empty() const;
int min() const; //return the smallest item
};
#endif
main:
Code:
#include <iostream>
#include "priorityQueue.h"
using namespace std;
int main()
{
priorityQueue myqueue; //class object
if (myqueue.empty())
cout << "My priority Queue is empty\n" << endl; //prompt
myqueue.insert(59); //Insert value into queue
cout << "After inserting 59 Priority Queue has" << endl;
myqueue.heapify(59);
myqueue.printArray(data);
myqueue.insert(41);
cout << "After inserting 41 Priority Queue has" << endl;
myqueue.heapify(41);
myqueue.printArray(data);
myqueue.insert(25);
cout << "After inserting 25 Priority Queue has" << endl;
myqueue.heapify(25);
myqueue.printArray(data);
myqueue.insert(12);
cout << "After inserting 12 Priority Queue has" << endl;
myqueue.heapify(12);
myqueue.printArray(data);
myqueue.insert(91);
cout << "After inserting 91 Priority Queue has" << endl;
myqueue.heapify(91);
myqueue.printArray(data);
myqueue.min();
myqueue.extractMin();
cout << "After extracting the minimum value Priority Queue has" << endl;
myqueue.printArray(data);
myqueue.insert(34);
cout << "After inserting 34 Priority Queue has" << endl;
myqueue.heapify(34);
myqueue.printArray(data);
myqueue.insert(63);
cout << "After inserting 63 Priority Queue has" << endl;
myqueue.heapify(63);
myqueue.printArray(data);
myqueue.extractMin();
cout << "After extracting the minimum value Priority Queue has" << endl;
myqueue.printArray(data);
myqueue.insert(75);
cout << "After inserting 75 Priority Queue has" << endl;
myqueue.heapify(75);
myqueue.printArray(data);
myqueue.insert(85);
cout << "After inserting 85 Priority Queue has" << endl;
myqueue.heapify(85);
myqueue.printArray(data);
myqueue.extractMin();
cout << "After extracting the minimum value Priority Queue has" << endl;
myqueue.printArray(data);
cout <<"Minimum value is " ;
cout << myqueue.min() <<endl; //prints out heap min
system("pause");
return 0;
}
priorityQueue::priorityQueue() //constructor
{
size = CAPACITY;
data = new int[size];
}
priorityQueue::~priorityQueue() //destructor
{
}
int priorityQueue::getParent(int index) //finds parent
{
return (index - 1) / 2;
}
int priorityQueue::getLeftChild(int index) //finds left child
{
return (2 * index) + 1;
}
int priorityQueue::getRightChild(int index) //find right child
{
return (2 * index) + 2;
}
void priorityQueue::swap(int& item1, int& item2) //swaps value of two variables
{
int temp = item1;
item1 = item2;
item2 = temp;
}
void priorityQueue::heapify(int index)
{
int largest = index;
int l = getLeftChild(index);
int r = getRightChild(index);
if (l < size && data[l] > data[index])
{
largest = l;
}
if (r < size && data[r] > data[largest])
{
largest = r;
}
if (largest != index)
{
swap(data[index], data[largest]);
heapify(largest);
}
}
void priorityQueue::printArray(int [])
{
for (int i = 0; i < size; i++)
{
cout << data[i] << ", ";
}
}
int priorityQueue::extractMin() //finds min and removes it
{
int min = data[0];
data[0] = data[size - 1];
size -= 1;
heapify(0);
return min;
}
int priorityQueue::min() const // finds min
{
return data[0];
}
bool priorityQueue::empty() const // checks if heap is empty
{
if (data == NULL)
{
return true;
}
else
{
return false;
}
}
void priorityQueue::insert(int item)
{
size += 1;
int i = size - 1;
while (i > 0 && data[getParent(i)] < item)
{
data[i] = data[getParent(i)];
i = getParent(i);
}
data[i] = item;
}
Last edited by Bankaijoey; April 23rd, 2016 at 10:41 AM.
-
April 23rd, 2016, 10:56 AM
#2
Re: Parameters are not being accepted for my printArray function
Originally Posted by Bankaijoey
When I try to call printArray with data it will not let me compile or run my program I don't know what else would go in the parameters to allow the program to work. Anything helps thanks.
What compiler error do you get? At which line?
Victor Nijegorodov
-
April 23rd, 2016, 11:00 AM
#3
Re: Parameters are not being accepted for my printArray function
"no instance of overloaded function "data" matches the required type" and
"Error C2664 'void priorityQueue:: printArray(int [])': cannot convert argument 1 from 'overloaded-function' to 'int []" on each line i call printArray
-
April 23rd, 2016, 11:11 AM
#4
Re: Parameters are not being accepted for my printArray function
Well, what if you'd try to change the printArray singature from
Code:
void priorityQueue::printArray(int [])
to
Code:
void priorityQueue::printArray(int*)
Victor Nijegorodov
-
April 23rd, 2016, 12:08 PM
#5
Re: Parameters are not being accepted for my printArray function
myqueue.printArray(data);
Where is data defined - and as what?
The private data member of priorityQueue class can't be accessed outside of the class is it's private. You haven't provided the code for the implementation of the class so we don't know what is expected of printArray(). If it is to display the queue data, then it probably doesn't need a parameter. If it is to populate an array with the queue data, then an appropriate definition of data will need to be provided in main().
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
April 25th, 2016, 03:27 AM
#6
Re: Parameters are not being accepted for my printArray function
Code:
myqueue.printArray(data);
data is a member of priorityQueue. printArray ia a member function of priorityQueue. So I see no point to pass data to printArra as a function parameter, as data is already in class scope and accessible for printArray. Just make it be:
Best regards,
Igor
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|