How to write a class that uses a structure as a private datatype and uses it's pointers in the implementation.

class PQueue{
public :
/*other public functions and the constructors,destructors*/
private :
struct chunkT{
vector<int> nums ;
chunkT *next ;
};
/*other private functions*/
};



The implementation code uses the struct chunkT to generate a linked lists of the given type but gives many errors .

basically , the error messages reported demonstrate that the compiler did not identify the data type : chunkT

Please suggest a reason for this behaviour or give a sample function to be written in the implementation that uses a pointer to the struct chunkT .

many thanks in advance . :)
Last edited on
Make sure that you define the chunkT type before using it.
I have defined it in the interface . Is it not sufficient ?
It depends on where you use it. If you declare a member function that takes a chunkT argument in the public section above it will fail to compile because chunkT has not yet been defined.
A public method that uses a private structure, ¿how that could work?
@OP: provide a minimal example
peter - I will write the function in the private part only and not in the public part .

ne - The private structure is needed to maintain the internal data processing and the function using the struct type will also be in the private section !

The code you posted compiles without error.

Please post the code that is causing the error.
IMPLEMENTATION FILE :




#include "pqueue.h"
#include "genlib.h"
#include <iostream>
#include "vector.h"

//const int MAXSIZE = 4 ;

PQueue::PQueue()
{
first = new chunkT ;
first->next = NULL ;
}


PQueue::~PQueue()
{
}


bool PQueue::isEmpty()
{
return (first == NULL) ;
}

int PQueue::size()
{
int size = 0 ;
for(PQueue ::chunkT *PQueue ::pntr = first; pntr!=NULL; pntr = pntr->next){
size += pntr->numbers.size() ;
}
return size ;
}


/* Implementation notes: enqueue
* -----------------------------
* Since we're keeping the vector in no particular order, we just append this
* new element to the end. It's the easiest/fastest thing to do.
*/
void PQueue::enqueue(int newValue)
{
addNumber(first,newValue ) ;
}


/* Implementation notes: dequeueMax
* --------------------------------
* Since we're keeping the vector in no particular order, we have to search to
* find the largest element. Once found, we remove it from the vector and
* return that value.
*/
int PQueue::dequeueMax()
{
if (isEmpty())
Error("Tried to dequeue max from an empty pqueue!");
int num = first->numbers[0] ;
first->numbers.removeAt(0) ;
if(first->numbers.size() == 0){
first = first->next ;
}
return num ;
}

int PQueue::bytesUsed()
{
return 0;
}


string PQueue::implementationName()
{
return "sorted chunk list";
}

void PQueue::printDebuggingInfo()
{
cout << "------------------ START DEBUG INFO ------------------" << endl;
cout << "Pqueue contains " << entries.size() << " entries" << endl;
printChunkList(first) ;
cout << endl;
cout << "------------------ END DEBUG INFO ------------------" << endl;
}









const int MAXSIZE = 4 ;

void PQueue ::addNumber(chunkT *&chunkPntr, int num ){
if(chunkPntr->numbers.size() == 0){
chunkPntr->numbers.add(num) ;
}else{
int firstNum = chunkPntr->numbers[0] ;
int lastNum = chunkPntr->numbers[chunkPntr->numbers.size() - 1] ;

if(num >= firstNum){
modify(chunkPntr,num) ;
}else if(num > lastNum){
modify(chunkPntr,num) ;
}else if(num == lastNum){// case when the number to be added equals the minimum number in present array
if(chunkPntr->numbers.size() >= 4){
allocateNewBlockAndAdd(chunkPntr,num) ;
putNumberInArray(chunkPntr->next->numbers,num) ;
}else{
putNumberInArray(chunkPntr->numbers,num) ;
}
}else{
if(chunkPntr->next != NULL){
addNumber(chunkPntr->next,num) ;
}else{
if(chunkPntr->numbers.size() >= 4){
allocateNewBlockAndAdd(chunkPntr,num) ;
chunkPntr->next->numbers.add(num) ;
}else{
chunkPntr->numbers.add(num) ;
}
}
}
}
}


void PQueue ::modify(chunkT *&pntr , int num){
if(pntr->numbers.size() < MAXSIZE){
putNumberInArray(pntr->numbers,num) ;
}else{
allocateNewBlockAndAdd(pntr,num) ;
if(pntr->numbers[pntr->numbers.size()-1] < num){
putNumberInArray(pntr->numbers,num);
}else{
putNumberInArray(pntr->next->numbers,num) ;
}
}
}


void PQueue ::putNumberInArray(Vector<int> &arr,int num){
for(int i = 0 ; i < arr.size() ; i++ ) {
if(arr[i] < num){
arr.insertAt(i,num) ;
break ;
}else if(arr[i] == num){
arr.insertAt(i,num) ;
break ;
}
}
}


void PQueue ::allocateNewBlockAndAdd(chunkT *&pntr, int num){
chunkT *oldLink = pntr->next ;
pntr ->next = new chunkT ;
divideNumbers(pntr) ;
pntr->next->next = oldLink ;
}


void PQueue ::divideNumbers(chunkT *&current){
Vector<int> newVec ;
int size = current->numbers.size() ;
for(int i = size/2 ; i < size ; i++ ) {
newVec.add( current->numbers[i] ) ;
}

for(int i = 0 ; i < size/2 ; i++ ){
current->numbers.removeAt(current->numbers.size() - 1) ;
}

current->next->numbers = newVec ;
}


void PQueue ::printChunkList(chunkT *&first){
if(first != NULL){
cout << "\n" << endl ;
printVector(first->numbers) ;
printChunkList(first->next) ;
cout << "nikunj banka" << endl ;
}
}


void PQueue ::printVector(Vector<int> numbers){
for(int i = 0 ; i < numbers.size() ; i++ ) {
cout << "at index" << i << " -> ";
cout << numbers[i] << endl ;
}
}




INTERFACE FILE :
/*
* File: pqueue.h
* --------------
* Defines the interface for the priority queue class.
*
* Julie Zelenski, CS106, Fall 2007
*/


#ifndef _pqueue_h
#define _pqueue_h

#include "genlib.h"
#include "vector.h"
#include "disallowcopy.h"

/*
* Class: PQueue
* -------------
* This is the class for a priority queue. This is not
* simple FIFO queue, it is a priority queue, where elements are
* retrieved in order of priority, not just by longevity in queue.
* The elements are integers and the integer is assumed to represent
* the priority (larger integer is higher priority).
*/
class PQueue
{
public:

/*
* Constructor: PQueue
* Usage: PQueue pq;
* PQueue *ppq = new PQueue;
* ---------------------------------
* Initializes a new pqueue to have no elements.
*/
PQueue();


/*
* Destructor: ~PQueue
* Usage: delete ppq;
* ------------------
* Deallocates all the storage associated with this pqueue.
*/
~PQueue();


/*
* Member function: isEmpty
* Usage: if (pq.isEmpty()) . . .
* -------------------------------
* Returns true if this pqueue contains no elements.
*/
bool isEmpty();


/*
* Member function: size
* Usage: nElemes = pq.size();
* ---------------------------
* Returns number of elements contained in this pqueue.
*/
int size();


/*
* Member function: enqueue
* Usage: pq.enqueue(val);
* -----------------------
* Adds the specified element to this pqueue. No effort is made to
* avoid duplicates.
*/
void enqueue(int newElem);


/*
* Member function: eequeueMax
* Usage: maxElem = pq.dequeueMax();
* ---------------------------------
* Removes the largest priority element from this pqueue and returns it.
* If this pqueue is empty, this function raises an error.
*/
int dequeueMax();


/*
* Member function: bytesUsed
* Usage: numBytes = pq.bytesUsed();
* ----------------------------------
* This function would not usually be included as part of the class,
* but this is here as part of evaluating the tradeoffs betweem
* implementations. Given a pqueue, this function counts up
* and return the total amount of space used given its
* current contents.
*/
int bytesUsed();


/*
* Member function: implementationName
* Usage: cout << pq.implementationName();
* ---------------------------------------
* This operation would not usually be included as part of the class
* class, but is included to help with analyzing and reporting results.
* This member function returns a string that describes the PQueue
* implementation strategy ("sorted linked list", "vector", etc.).
*/
string implementationName();


/*
* Member function: printDebuggingInfo
* Usage: pq.printDebuggingInfo();
* -------------------------------
* This operation would not usually be included as part of the class,
* but is included to give you a hook to put any helpful debugging
* print code (for example, something that prints the goopy details of
* the internal structure). You don't need to implement this routine and can
* ignore it entirely, but you may find it helpful to use as you are
* doing development.
*/
void printDebuggingInfo();



private:
struct chunkT*{
Vector<int> numbers ;
chunkT *next ;
};

chunkT *first ;
void addNumber(chunkT *&chunkPntr, int num ) ;
void modify(chunkT *&pntr , int num) ;
void putNumberInArray(Vector<int> &arr,int num) ;
void allocateNewBlockAndAdd(chunkT *&pntr, int num) ;
void divideNumbers(chunkT *&current) ;
void printVector(Vector<int> numbers) ;
void printChunkList(chunkT *&first) ;

DISALLOW_COPYING(PQueue)
};


#endif
Dude, PLEASE use code tags
1
2
3
4
5
private:
   struct chunkT*{ //<-- asterisk (you have it, it shouldn't be there)
      Vector<int> numbers ; 
      chunkT *next ; 
   };


1
2
//PQueue::size
   for(PQueue ::chunkT *PQueue ::pntr = first; //¿what? 

Last edited on
The correct header file is
 
#include <vector> 


I didn't see a #include <string>

When declaring the structure, it should be declared without the *.
Also, vector should be declared as lower case, not with a leading cap (numerous places).
1
2
3
4
struct chunkT 
{	vector<int> numbers ; 
	chunkT * next ; 
};


In PQueue::size, your iterator is not declared correctly. You're declaring that pntr has global scope.
1
2
3
4
5
6
7
8
int PQueue::size()
{
int size = 0 ; 
for(PQueue::chunkT * pntr = first; pntr!=NULL; pntr = pntr->next){
size += pntr->numbers.size() ; 
}
return size ; 
}


There are more errors in the .cpp file, but that should get you started.
@AbstractionAnon: there are other libraries.
Topic archived. No new replies allowed.