I am trying to do a priority queue with a custom type, but I keep getting this error

des.cpp:77: error: expected init-declarator before "eventQ"
des.cpp:77: error: expected `;' before "eventQ"

This happens in the following code

Program: This is a discrete event simulator designed to aid in the analysis
 * of particular scheduling algorithms for comparison of efficiency
 */

//the following macro generates a random number between A and B
#define RAND_RANGE(A,B) ( (A) + ((B)-(A))*(double)rand( ) / RAND_MAX )
#include <time.h>
#include <errno.h>
#include <limits.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/times.h>
#include <sys/stat.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/socket.h>
#include <signal.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <string>
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <vector>
#include <queue>

using namespace std;

enum evTyp { PARVL = 0, CPUEND = 1, IOEND = 2};

/*
   The strtTime is a random number generated by the RAND_RANGE for range 0-300
   The endTime is a random number generated by RAND_RANGE( for range 1-60
   When the CPUtime + IOtime become >= to the endTime, then after the next CPU burst event
   is logged, the process is ended
   The CPUtime effectively stores the time of a CPU burst determined by randCPU_Burst function
   The IOtime effectively stores the IO time that the process waited in the IOqueue
   IOqStrt is used for time it gets into the IO queue
*/

struct Evnt
{
    int pid;
    evTyp type;
    double strtTime;
    double endTime;
    double evTime;
    double CPUtime;
    double IOtime;
    double IOqStrt;
};


Evnt operator<(const Evnt& x, const Evnt& y)
{
    if(x.evTime < y.evTime)
    {
	return x;
    }
    return y;
}


template < 
class Type, 
class Container=vector<Type>,
class Compare=less<typename Container::value_type> 
	 >
class priority_queue

priority_queue<Evnt, vector<Evnt>,less<vector<Evnt>::value_type> > eventQ;

that last line of the code is line 77 where the error is occurring. I have no idea what I'm doing wrong. I thought maybe I was using a predefined name type when I had it called Event, but changing it to Evnt still didn't fix the trouble. I just dunno what is wrong with it, and this error explanation doesn't make sense to me.

Recommended Answers

All 5 Replies

The syntax problem is that you failed to terminate your forward declaration with a semicolon. But since you've already included <queue>, there's little point in a forward declaration anyway, and it'll cause you more problems. Remove this part and the code will compile:

template < 
class Type, 
class Container=vector<Type>,
class Compare=less<typename Container::value_type> 
	 >
class priority_queue

The syntax problem is that you failed to terminate your forward declaration with a semicolon. But since you've already included <queue>, there's little point in a forward declaration anyway, and it'll cause you more problems. Remove this part and the code will compile:

template < 
class Type, 
class Container=vector<Type>,
class Compare=less<typename Container::value_type> 
	 >
class priority_queue

Wooo! It did NOT like that. It threw up this when I took it out.

/usr/lib/gcc/i386-redhat-linux/3.4.4/../../../../include/c++/3.4.4/bits/stl_function.h: In member function `bool std::less<_Tp>::operator()(const _Tp&, const _Tp&) const [with _Tp = Evnt]':
/usr/lib/gcc/i386-redhat-linux/3.4.4/../../../../include/c++/3.4.4/bits/stl_heap.h:279: instantiated from `void std::__adjust_heap(_RandomAccessIterator, _Distance, _Distance, _Tp, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Evnt*, std::vector<Evnt, std::allocator<Evnt> > >, _Distance = int, _Tp = Evnt, _Compare = std::less<Evnt>]'
/usr/lib/gcc/i386-redhat-linux/3.4.4/../../../../include/c++/3.4.4/bits/stl_heap.h:404: instantiated from `void std::make_heap(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Evnt*, std::vector<Evnt, std::allocator<Evnt> > >, _Compare = std::less<Evnt>]'
/usr/lib/gcc/i386-redhat-linux/3.4.4/../../../../include/c++/3.4.4/bits/stl_queue.h:369: instantiated from `std::priority_queue<_Tp, _Sequence, _Compare>::priority_queue(const _Compare&, const _Sequence&) [with _Tp = Evnt, _Sequence = std::vector<Evnt, std::allocator<Evnt> >, _Compare = std::less<Evnt>]'
des.cpp:79: instantiated from here
/usr/lib/gcc/i386-redhat-linux/3.4.4/../../../../include/c++/3.4.4/bits/stl_function.h:227: error: cannot convert `Evnt' to `bool' in return
make: *** [des.o] Error 1

So...that scares me horribly(I may have soiled myself...j/k)....Any suggestions?

>So...that scares me horribly(I may have soiled myself...j/k)
Welcome to the standard library. :) Let's shrink it down a bit:

des.cpp:79:
    error: cannot convert `Evnt' to `bool'

What's around line 79 that suggests you're in any way using an Evnt object in boolean context?

>So...that scares me horribly(I may have soiled myself...j/k)
Welcome to the standard library. :) Let's shrink it down a bit:

des.cpp:79:
    error: cannot convert `Evnt' to `bool'

What's around line 79 that suggests you're in any way using an Evnt object in boolean context?

Line 79 is the declaration... It became 79 from 77 because i put two comments around the template delcaration.

priority_queue<Evnt,vector<Evnt>,less<vector<Evnt>::value_type>> eventQ;

No clue...

Line 79 is the declaration... It became 79 from 77 because i put two comments around the template delcaration.

priority_queue<Evnt,vector<Evnt>,less<vector<Evnt>::value_type>> eventQ;

No clue...

AHA! I figured it out. Apparently when you overload the < operator it has to return a bool...I didn't know this being new to C and all, so I changed the definition of the operator< and it compiles now....only hope it returns what I want it to return...

Thanks for your help. I salute you Narue!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.