Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Student assistance requested.

Reply
Thread Tools

Student assistance requested.

 
 
sandy@murdocks.on.ca
Guest
Posts: n/a
 
      10-28-2006
I am working on an assignment which is a 'discrete event simulation'. I
have a class called event, I need to add it to a priority queue and I
cannot for some reason. I am sure it's the syntax...

Basically I am going to loop through 100 times and create events, first
they go to the priority Queue, then as the time for the event happens I
will move them to 'regular' queues. I can't insert into my Priority
queue.

Here is the header for the Priority Queue:
<code>
#ifndef PQUEUEBH_HPP
#define PQUEUEBH_HPP

// Priority Queue: Header File

template <class Etype>

// Assumption: Etype is defined on the operation <

class PQueue
{
public:

// Constructor
//
// Input : None
// Purpose: To create an empty Priority Queue
// Output : None

PQueue ( );


// Copy constructor I
//
// Input : Priority Queue PQ
// Purpose: To initialize Priority Queue to PQ
// Output : None

PQueue ( const PQueue & PQ );


// Copy constructor II
//
// Input : Array A of length n
// Purpose: To initialize Priority Queue to the elements of A
// Output : None

PQueue ( Etype A[], int n );


// Destructor
//
// Input : None
// Purpose: To free memory of Priority Queue
// Output : None

~PQueue ( );


// Copy assignment
//
// Input : Priority Queue PQ
// Purpose: To assign PQ to current Priority Queue
// Output : Current Priority Queue

const PQueue & operator= ( const PQueue & PQ );


// Insert
//
// Input : Element E
// Purpose: To place E into the Priority Queue
// Output : 1 if successful; 0 otherwise

int Insert ( const Etype & E );


// DeleteMin
//
// Input : None
// Purpose: To return the highest priority element E and
// To delete E from the Priority Queue
// Output : Element E and 1 if successful; 0 otherwise

int DeleteMin ( Etype & E );


// Length
//
// Input : None
// Purpose: To the return the current size of the Priority Queue
// Output : Current size of Priority Queue

int Length ( ) const;


// Empty
//
// Input : None
// Purpose: To check if Priority Queue is empty
// Output : 1 if empty; 0 otherwise

int Empty ( ) const;


// Clear
//
// Input : None
// Purpose: To re-initialize Priority Queue to empty
// Output : None

void Clear ( );


// Overloaded ==

int operator== ( const PQueue & PQ ) const;

private:

// DoubleArray
//
// Input : None
// Purpose: To double the maximum size of Priority Queue
// Output : None

void DoubleArray ( );


// FixHeap
//
// Input : None
// Purpose: To restore the heap-order property
// of the binary heap
// Output : None

void FixHeap ( );


// PercolateDown
//
// Input : Position i
// Purpose: To restore the heap-order property
// of the binary heap rooted at position i
// Output : None

void PercolateDown ( int i );


// PercolateUp
//
// Input : Position i
// Purpose: To restore the heap-order property
// of the binary heap along the path from i to the root
// Output : None

void PercolateUp ( int i );


// Data Members


// Current and maximum size of Priority Queue
int CurrentSize, MaxSize;

// Dynamic binary heap to store the elements of Priority Queue
Etype *Array;

};

#endif
</code>

here is my function where I am creating an event and trying to insert
it:

<code>
void RunSim()
{
typedef Queue<Event> Q1, Q2, Q3, Q4;
typedef PQueue<Event> PQ;

Event *E;

// put the first Arrival into the PQ
E = new Event;
// interval is 1 to 5 params: Event Number, Time (int), Type A or C
E->LoadEvent(0, GenRandom(5), 'A');
PQ.Insert(E);

}
</code>

I keep getting told that it expects a semi-colon before the
PQ.Insert(E);

If I comment out that line it compiles and runs, so the semi-colon is
not the problem; but I have no idea what is...

The more I program with C++ the more I like vb.net...

I am hoping it's something stupid simple that just needs another pair
of eyes.

Thanks for your assistance.

 
Reply With Quote
 
 
 
 
Daniel T.
Guest
Posts: n/a
 
      10-28-2006
"" <> wrote:

> I am working on an assignment which is a 'discrete event simulation'. I
> have a class called event, I need to add it to a priority queue and I
> cannot for some reason. I am sure it's the syntax...
>
> Basically I am going to loop through 100 times and create events, first
> they go to the priority Queue, then as the time for the event happens I
> will move them to 'regular' queues. I can't insert into my Priority
> queue.
>
> Here is the header for the Priority Queue:
> <code>
> #ifndef PQUEUEBH_HPP
> #define PQUEUEBH_HPP
>
> // Priority Queue: Header File
>
> template <class Etype>
>
> // Assumption: Etype is defined on the operation <
>
> class PQueue
> {
> public:
>
> // Constructor
> //
> // Input : None
> // Purpose: To create an empty Priority Queue
> // Output : None
>
> PQueue ( );
>
>
> // Copy constructor I
> //
> // Input : Priority Queue PQ
> // Purpose: To initialize Priority Queue to PQ
> // Output : None
>
> PQueue ( const PQueue & PQ );
>
>
> // Copy constructor II
> //
> // Input : Array A of length n
> // Purpose: To initialize Priority Queue to the elements of A
> // Output : None
>
> PQueue ( Etype A[], int n );
>
>
> // Destructor
> //
> // Input : None
> // Purpose: To free memory of Priority Queue
> // Output : None
>
> ~PQueue ( );
>
>
> // Copy assignment
> //
> // Input : Priority Queue PQ
> // Purpose: To assign PQ to current Priority Queue
> // Output : Current Priority Queue
>
> const PQueue & operator= ( const PQueue & PQ );
>
>
> // Insert
> //
> // Input : Element E
> // Purpose: To place E into the Priority Queue
> // Output : 1 if successful; 0 otherwise
>
> int Insert ( const Etype & E );
>
>
> // DeleteMin
> //
> // Input : None
> // Purpose: To return the highest priority element E and
> // To delete E from the Priority Queue
> // Output : Element E and 1 if successful; 0 otherwise
>
> int DeleteMin ( Etype & E );
>
>
> // Length
> //
> // Input : None
> // Purpose: To the return the current size of the Priority Queue
> // Output : Current size of Priority Queue
>
> int Length ( ) const;
>
>
> // Empty
> //
> // Input : None
> // Purpose: To check if Priority Queue is empty
> // Output : 1 if empty; 0 otherwise
>
> int Empty ( ) const;
>
>
> // Clear
> //
> // Input : None
> // Purpose: To re-initialize Priority Queue to empty
> // Output : None
>
> void Clear ( );
>
>
> // Overloaded ==
>
> int operator== ( const PQueue & PQ ) const;
>
> private:
>
> // DoubleArray
> //
> // Input : None
> // Purpose: To double the maximum size of Priority Queue
> // Output : None
>
> void DoubleArray ( );
>
>
> // FixHeap
> //
> // Input : None
> // Purpose: To restore the heap-order property
> // of the binary heap
> // Output : None
>
> void FixHeap ( );
>
>
> // PercolateDown
> //
> // Input : Position i
> // Purpose: To restore the heap-order property
> // of the binary heap rooted at position i
> // Output : None
>
> void PercolateDown ( int i );
>
>
> // PercolateUp
> //
> // Input : Position i
> // Purpose: To restore the heap-order property
> // of the binary heap along the path from i to the root
> // Output : None
>
> void PercolateUp ( int i );
>
>
> // Data Members
>
>
> // Current and maximum size of Priority Queue
> int CurrentSize, MaxSize;
>
> // Dynamic binary heap to store the elements of Priority Queue
> Etype *Array;
>
> };
>
> #endif
> </code>
>
> here is my function where I am creating an event and trying to insert
> it:
>
> <code>
> void RunSim()
> {
> typedef Queue<Event> Q1, Q2, Q3, Q4;
> typedef PQueue<Event> PQ;
>
> Event *E;
>
> // put the first Arrival into the PQ
> E = new Event;
> // interval is 1 to 5 params: Event Number, Time (int), Type A or C
> E->LoadEvent(0, GenRandom(5), 'A');
> PQ.Insert(E);
>
> }
> </code>
>
> I keep getting told that it expects a semi-colon before the
> PQ.Insert(E);


PQ is not an object, it is a class (strictly speaking a typedef of a
class.) You need to first create an object, then insert into it...

PQ myPQ;
myPQ.Insert( E );

> I am hoping it's something stupid simple that just needs another pair
> of eyes.


Yep. So simple that even the compiler was surprised.

--
To send me email, put "sheltie" in the subject.
 
Reply With Quote
 
 
 
 
sandy@murdocks.on.ca
Guest
Posts: n/a
 
      10-28-2006

Daniel T. wrote:
> "" <> wrote:
>
> > I am working on an assignment which is a 'discrete event simulation'. I
> > have a class called event, I need to add it to a priority queue and I
> > cannot for some reason. I am sure it's the syntax...
> >
> > Basically I am going to loop through 100 times and create events, first
> > they go to the priority Queue, then as the time for the event happens I
> > will move them to 'regular' queues. I can't insert into my Priority
> > queue.
> >
> > Here is the header for the Priority Queue:
> > <code>
> > #ifndef PQUEUEBH_HPP
> > #define PQUEUEBH_HPP
> >
> > // Priority Queue: Header File
> >
> > template <class Etype>
> >
> > // Assumption: Etype is defined on the operation <
> >
> > class PQueue
> > {
> > public:
> >
> > // Constructor
> > //
> > // Input : None
> > // Purpose: To create an empty Priority Queue
> > // Output : None
> >
> > PQueue ( );
> >
> >
> > // Copy constructor I
> > //
> > // Input : Priority Queue PQ
> > // Purpose: To initialize Priority Queue to PQ
> > // Output : None
> >
> > PQueue ( const PQueue & PQ );
> >
> >
> > // Copy constructor II
> > //
> > // Input : Array A of length n
> > // Purpose: To initialize Priority Queue to the elements of A
> > // Output : None
> >
> > PQueue ( Etype A[], int n );
> >
> >
> > // Destructor
> > //
> > // Input : None
> > // Purpose: To free memory of Priority Queue
> > // Output : None
> >
> > ~PQueue ( );
> >
> >
> > // Copy assignment
> > //
> > // Input : Priority Queue PQ
> > // Purpose: To assign PQ to current Priority Queue
> > // Output : Current Priority Queue
> >
> > const PQueue & operator= ( const PQueue & PQ );
> >
> >
> > // Insert
> > //
> > // Input : Element E
> > // Purpose: To place E into the Priority Queue
> > // Output : 1 if successful; 0 otherwise
> >
> > int Insert ( const Etype & E );
> >
> >
> > // DeleteMin
> > //
> > // Input : None
> > // Purpose: To return the highest priority element E and
> > // To delete E from the Priority Queue
> > // Output : Element E and 1 if successful; 0 otherwise
> >
> > int DeleteMin ( Etype & E );
> >
> >
> > // Length
> > //
> > // Input : None
> > // Purpose: To the return the current size of the Priority Queue
> > // Output : Current size of Priority Queue
> >
> > int Length ( ) const;
> >
> >
> > // Empty
> > //
> > // Input : None
> > // Purpose: To check if Priority Queue is empty
> > // Output : 1 if empty; 0 otherwise
> >
> > int Empty ( ) const;
> >
> >
> > // Clear
> > //
> > // Input : None
> > // Purpose: To re-initialize Priority Queue to empty
> > // Output : None
> >
> > void Clear ( );
> >
> >
> > // Overloaded ==
> >
> > int operator== ( const PQueue & PQ ) const;
> >
> > private:
> >
> > // DoubleArray
> > //
> > // Input : None
> > // Purpose: To double the maximum size of Priority Queue
> > // Output : None
> >
> > void DoubleArray ( );
> >
> >
> > // FixHeap
> > //
> > // Input : None
> > // Purpose: To restore the heap-order property
> > // of the binary heap
> > // Output : None
> >
> > void FixHeap ( );
> >
> >
> > // PercolateDown
> > //
> > // Input : Position i
> > // Purpose: To restore the heap-order property
> > // of the binary heap rooted at position i
> > // Output : None
> >
> > void PercolateDown ( int i );
> >
> >
> > // PercolateUp
> > //
> > // Input : Position i
> > // Purpose: To restore the heap-order property
> > // of the binary heap along the path from i to the root
> > // Output : None
> >
> > void PercolateUp ( int i );
> >
> >
> > // Data Members
> >
> >
> > // Current and maximum size of Priority Queue
> > int CurrentSize, MaxSize;
> >
> > // Dynamic binary heap to store the elements of Priority Queue
> > Etype *Array;
> >
> > };
> >
> > #endif
> > </code>
> >
> > here is my function where I am creating an event and trying to insert
> > it:
> >
> > <code>
> > void RunSim()
> > {
> > typedef Queue<Event> Q1, Q2, Q3, Q4;
> > typedef PQueue<Event> PQ;
> >
> > Event *E;
> >
> > // put the first Arrival into the PQ
> > E = new Event;
> > // interval is 1 to 5 params: Event Number, Time (int), Type A or C
> > E->LoadEvent(0, GenRandom(5), 'A');
> > PQ.Insert(E);
> >
> > }
> > </code>
> >
> > I keep getting told that it expects a semi-colon before the
> > PQ.Insert(E);

>
> PQ is not an object, it is a class (strictly speaking a typedef of a
> class.) You need to first create an object, then insert into it...
>
> PQ myPQ;
> myPQ.Insert( E );
>
> > I am hoping it's something stupid simple that just needs another pair
> > of eyes.

>
> Yep. So simple that even the compiler was surprised.
>
> --
> To send me email, put "sheltie" in the subject.


Thanks... I am still having trouble with it though...

I am using this function now:
<code>
void RunSim()
{
int Result;
typedef Queue<Event> Q1, Q2, Q3, Q4;
typedef PQueue<Event> PQ;
PQ myPQ;
Event *E;

// put the first Arrival into the PQ
E = new Event;
// interval is 1 to 5
E->LoadEvent(0, GenRandom(5), 'A');
Result = myPQ.Insert(E);

}
</code>

and I now get this:
no matching function for call to `PQueue<Event>::Insert(Event**)'
candidates are: int PQueue<Etype>::Insert(const Etype&) [with Etype =
Event]

As you can see in the header there IS a function Insert...

 
Reply With Quote
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      10-28-2006
wrote:

[implementation of PQueue and previous conversation snipped]
> I am using this function now:
> <code>
> void RunSim()
> {
> int Result;
> typedef Queue<Event> Q1, Q2, Q3, Q4;
> typedef PQueue<Event> PQ;
> PQ myPQ;
> Event *E;
>
> // put the first Arrival into the PQ
> E = new Event;
> // interval is 1 to 5
> E->LoadEvent(0, GenRandom(5), 'A');
> Result = myPQ.Insert(E);
>
> }
> </code>
>
> and I now get this:
> no matching function for call to `PQueue<Event>::Insert(Event**)'
> candidates are: int PQueue<Etype>::Insert(const Etype&) [with Etype =
> Event]
> As you can see in the header there IS a function Insert...


You are trying to insert an Event* into a queue of Event. You have two
alternatives:

a) use PQueue<Event*> instead of PQueue<Event>.
b) use myPQ.insert(*E) instead of myPQ.insert(E).

Which one to choose depends on whether you want value or reference semantics
for the queue and on whether the contents of the queue needs to be
polymorphic. If you need to go with (a), you need to pay close attention to
ownership issues and you also need to think about ranking pointers
according to their pointees.

BTW: is there a reason why you don't use the std::queue<> and
std:riority_queue<> templates?


Best

Kai-Uwe Bux
 
Reply With Quote
 
Daniel T.
Guest
Posts: n/a
 
      10-28-2006
In article <. com>,
"" <> wrote:

> > > // Insert
> > > //
> > > // Input : Element E
> > > // Purpose: To place E into the Priority Queue
> > > // Output : 1 if successful; 0 otherwise
> > >
> > > int Insert ( const Etype & E );


> I am using this function now:
> <code>
> void RunSim()
> {
> int Result;
> typedef Queue<Event> Q1, Q2, Q3, Q4;
> typedef PQueue<Event> PQ;
> PQ myPQ;
> Event *E;
>
> // put the first Arrival into the PQ
> E = new Event;
> // interval is 1 to 5
> E->LoadEvent(0, GenRandom(5), 'A');
> Result = myPQ.Insert(E);
>
> }
> </code>
>
> and I now get this:
> no matching function for call to `PQueue<Event>::Insert(Event**)'
> candidates are: int PQueue<Etype>::Insert(const Etype&) [with Etype =
> Event]
>
> As you can see in the header there IS a function Insert...


Insert takes an E reference, you are trying to pass an E pointer. Try
this instead:

Result = myPQ.Insert(*E);

--
To send me email, put "sheltie" in the subject.
 
Reply With Quote
 
sandy@murdocks.on.ca
Guest
Posts: n/a
 
      10-29-2006

Kai-Uwe Bux wrote:
> wrote:
>
> [implementation of PQueue and previous conversation snipped]
> > I am using this function now:
> > <code>
> > void RunSim()
> > {
> > int Result;
> > typedef Queue<Event> Q1, Q2, Q3, Q4;
> > typedef PQueue<Event> PQ;
> > PQ myPQ;
> > Event *E;
> >
> > // put the first Arrival into the PQ
> > E = new Event;
> > // interval is 1 to 5
> > E->LoadEvent(0, GenRandom(5), 'A');
> > Result = myPQ.Insert(E);
> >
> > }
> > </code>
> >
> > and I now get this:
> > no matching function for call to `PQueue<Event>::Insert(Event**)'
> > candidates are: int PQueue<Etype>::Insert(const Etype&) [with Etype =
> > Event]
> > As you can see in the header there IS a function Insert...

>
> You are trying to insert an Event* into a queue of Event. You have two
> alternatives:
>
> a) use PQueue<Event*> instead of PQueue<Event>.
> b) use myPQ.insert(*E) instead of myPQ.insert(E).
>
> Which one to choose depends on whether you want value or reference semantics
> for the queue and on whether the contents of the queue needs to be
> polymorphic. If you need to go with (a), you need to pay close attention to
> ownership issues and you also need to think about ranking pointers
> according to their pointees.
>
> BTW: is there a reason why you don't use the std::queue<> and
> std:riority_queue<> templates?
>
>
> Best
>
> Kai-Uwe Bux


As a student I am supposed to use what I am given. The prof gave us a
Queue and Priority Queue which we are to use.

I still have not got it working. When I use myPQ.insert(*E) I get:
[Linker error] undefined reference to `PQueue<Event>:Queue()'
[Linker error] undefined reference to `PQueue<Event>::Insert(Event
const&)'
[Linker error] undefined reference to `PQueue<Event>::~PQueue()'
[Linker error] undefined reference to `PQueue<Event>::~PQueue()'

When I use PQueue<Event*> I get the same.

The reason I am using the 'pointer' version of E when I declare my
Event is that I need to be able to create a new one with the same
'name', then insert it into the Priority Queue and make another one.
Rather than declare E1 - E100 I wanted to use 'new' to declare a new
one then tuck it away in the Queue.

 
Reply With Quote
 
sandy@murdocks.on.ca
Guest
Posts: n/a
 
      10-29-2006

wrote:
> Kai-Uwe Bux wrote:
> > wrote:
> >
> > [implementation of PQueue and previous conversation snipped]
> > > I am using this function now:
> > > <code>
> > > void RunSim()
> > > {
> > > int Result;
> > > typedef Queue<Event> Q1, Q2, Q3, Q4;
> > > typedef PQueue<Event> PQ;
> > > PQ myPQ;
> > > Event *E;
> > >
> > > // put the first Arrival into the PQ
> > > E = new Event;
> > > // interval is 1 to 5
> > > E->LoadEvent(0, GenRandom(5), 'A');
> > > Result = myPQ.Insert(E);
> > >
> > > }
> > > </code>
> > >
> > > and I now get this:
> > > no matching function for call to `PQueue<Event>::Insert(Event**)'
> > > candidates are: int PQueue<Etype>::Insert(const Etype&) [with Etype =
> > > Event]
> > > As you can see in the header there IS a function Insert...

> >
> > You are trying to insert an Event* into a queue of Event. You have two
> > alternatives:
> >
> > a) use PQueue<Event*> instead of PQueue<Event>.
> > b) use myPQ.insert(*E) instead of myPQ.insert(E).
> >
> > Which one to choose depends on whether you want value or reference semantics
> > for the queue and on whether the contents of the queue needs to be
> > polymorphic. If you need to go with (a), you need to pay close attention to
> > ownership issues and you also need to think about ranking pointers
> > according to their pointees.
> >
> > BTW: is there a reason why you don't use the std::queue<> and
> > std:riority_queue<> templates?
> >
> >
> > Best
> >
> > Kai-Uwe Bux

>
> As a student I am supposed to use what I am given. The prof gave us a
> Queue and Priority Queue which we are to use.
>
> I still have not got it working. When I use myPQ.insert(*E) I get:
> [Linker error] undefined reference to `PQueue<Event>:Queue()'
> [Linker error] undefined reference to `PQueue<Event>::Insert(Event
> const&)'
> [Linker error] undefined reference to `PQueue<Event>::~PQueue()'
> [Linker error] undefined reference to `PQueue<Event>::~PQueue()'
>
> When I use PQueue<Event*> I get the same.
>
> The reason I am using the 'pointer' version of E when I declare my
> Event is that I need to be able to create a new one with the same
> 'name', then insert it into the Priority Queue and make another one.
> Rather than declare E1 - E100 I wanted to use 'new' to declare a new
> one then tuck it away in the Queue.


I discovered an error in the way that I included my Priority Queue at
the top of main. It now compiles.

Thanks all!

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Student needs assistance... (deperately) sandy@murdocks.on.ca C++ 5 10-09-2006 06:02 PM
Student seeking assistance.... sandy@murdocks.on.ca C++ 10 10-06-2006 04:14 PM
Cisco Student VPN exercise problem : gen_unrfrag: fail to generate unreachable, unexpected args robert Cisco 0 06-02-2004 07:33 PM
SystemC : Can a CS student do it? GoodProggie101 VHDL 0 02-29-2004 06:15 PM
A student's question Chen Bin VHDL 7 09-04-2003 04:39 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57