CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Oct 2015
    Posts
    13

    Question 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.

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Parameters are not being accepted for my printArray function

    Quote Originally Posted by Bankaijoey View Post
    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

  3. #3
    Join Date
    Oct 2015
    Posts
    13

    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

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    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

  5. #5
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    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)

  6. #6
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    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:
    Code:
        void printArray();
    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
  •  





Click Here to Expand Forum to Full Width

Featured