0

我玩了很多不同的前向声明组合,这似乎是最好的。这是唯一一个编译的,直到我取消注释该行

e->process( this );

我从 xcode 收到一个错误,上面写着:

non-const lvalue reference to type 'Event::ModemSimV2' cannot bind to a temporary type 'ModemSimV2'

我真的不明白这是什么意思,任何帮助将不胜感激。

谢谢,

资源:

#include "ModemSimV2.h"

//xvxvxvxvxvxvxvxvxvxvxvxvxvxvxvxvxvxvxvxvxvxvxvxvxvxvxvxvxvxvxvxvx
//+++++++++++++++++++++++++++++ Event +++++++++++++++++++++++++++++

Event::Event(){

}

Event::Event( const Event &e ) {
    *this = e;
}

Event::~Event( ) {

}

/*
bool Event::operator > ( const Event & rhs ) const { 
    return time > rhs.time; 
}

bool Event::operator < ( const Event & rhs ) const { 
    return time < rhs.time; 
}

bool Event::operator <= ( const Event & rhs ) const { 
    return time < rhs.time; 
}

bool Event::operator != ( const Event & rhs ) const { 
    return time != rhs.time; 
}
 */

//xvxvxvxvxvxvxvxvxvxvxvxvxvxvxvxvxvxvxvxvxvxvxvxvxvxvxvxvxvxvxvxvx
//+++++++++++++++++++++++++++++ Dialin +++++++++++++++++++++++++++++

Dialin::Dialin (int name, int tm )
: time( tm ), who( name ) { 
    return;
}

Dialin::Dialin ( const Dialin &d ) {
    *this = d;
}

Dialin::~Dialin( ) {

}

void Dialin::process( ModemSimV2 &m ) {


}

//xvxvxvxvxvxvxvxvxvxvxvxvxvxvxvxvxvxvxvxvxvxvxvxvxvxvxvxvxvxvxvxvx
//++++++++++++++++++++++++++ EventHeap ++++++++++++++++++++++++++++


EventHeap::EventHeap( ) {

    size = 0;
}

EventHeap::EventHeap( int numVals ) { 

    size = 0;
}

//insert
void EventHeap::push( const Event e ) {

    *array[size] = e;
    reIndex( size );
    size++;
}

//removes the min val   
Event* EventHeap::pop( ) {

    Event *e = array[0];
    array[0] = array[size - 1];
    size--;
    if( !empty( ) )
        buildHeap(0);

    return e;
}

//re do
void EventHeap::buildHeap( int nodeIndex ) {

    int leftChildIndex, rightChildIndex, minIndex;
    Event *tmp;

    leftChildIndex = getLeft(nodeIndex);

    rightChildIndex = getRight(nodeIndex);

    if (rightChildIndex >= size) {

        if (leftChildIndex >= size)

            return;

        else

            minIndex = leftChildIndex;

    } else {

        if (array[leftChildIndex] <= array[rightChildIndex])

            minIndex = leftChildIndex;

        else

            minIndex = rightChildIndex;

    }

    if (array[nodeIndex] > array[minIndex]) {

        tmp = array[minIndex];

        array[minIndex] = array[nodeIndex];

        array[nodeIndex] = tmp;

        buildHeap(minIndex);

    }
}


//re index
void EventHeap::reIndex( int hole ) {

    while( array[hole] != NULL && array[hole] <  array[getParent( hole )] ) {
        int pIndex = getParent( hole );
        Event *temp( array[hole] );
        array[hole] = array[pIndex];
        array[pIndex] = temp;
        hole = pIndex;
    }
}

//is Empty
bool EventHeap::empty() const {
    return ( size == 0 );
}

int EventHeap::getLeft( int index ) const {
    return ( index * 2 ) + 1;
}

int EventHeap::getRight( int index ) const {
    return ( index * 2 ) + 2;
}

int EventHeap::getParent( int index ) const {
    return ( index - 1 ) / 2;
}

//xvxvxvxvxvxvxvxvxvxvxvxvxvxvxvxvxvxvxvxvxvxvxvxvxvxvxvxvxvxvxvxvx
//++++++++++++++++++++++++++ ModemSimV2 +++++++++++++++++++++++++++

// Constructor for ModemSim.
ModemSimV2::ModemSimV2( int modems, double avgLen, int callIntrvl, EventHeap e )
: freeModems( modems ), avgCallLen( avgLen ),
freqOfCalls( callIntrvl ), r( (int) time( 0 ) )
{
    eventSet = &e;
    nextCall( freqOfCalls );  // Schedule first call
}

// Place a new DIAL_IN event into the event queue.
// Then advance the time when next DIAL_IN event will occur.
// In practice, we would use a random number to set the time.
void ModemSimV2::nextCall( int delta ){
    static int nextCallTime = 0;
    static int userNum = 0;

    Event *e;
    Dialin d = Dialin( userNum++, nextCallTime ); 
    *e = d;
    eventSet->push( *e );
    nextCallTime += delta;
}

// Run the simulation until stopping time occurs.
void ModemSimV2::runSim( int stoppingTime ){
    Event *e;

    while( !eventSet->empty( ) ){
        e = eventSet->pop();
        if ( e->getTime() > stoppingTime )
            break;
        e->process( this );
        nextCall( freqOfCalls );
    }
}

标题:

#ifndef MODEM_SIM_V2_H
#define MODEM_SIM_V2_H

#include <queue>
#include <vector>
#include <functional>  // for greater()
#include <climits>     // for INT_MAX
#include <iostream>
#include "random.h"

using namespace std;

//xvxvxvxvxvxvxvxvxvxvxvxvxvxvxvxvxvxvxvxvxvxvxvxvxvxvxvxvxvxvxvxvx
//+++++++++++++++++++++++++++++ Event +++++++++++++++++++++++++++++

class Event{

protected:
    int who;        // the number of the user
    int time;       // when the event will occur
    int what;       // DIAL_IN or HANGUP
    class ModemSimV2;

public:
    Event( );
    Event( const Event &e );
    virtual ~Event( );

    bool operator > ( const Event & rhs ) const;
    bool operator < ( const Event & rhs ) const;
    bool operator <= ( const Event & rhs ) const;
    bool operator != ( const Event & rhs ) const;

    int getTime( ) { return time; };

    virtual void process( ModemSimV2 &m ) = 0;
};


 class Dialin : public Event{
 public:
     Dialin( int name = 0, int tm = 0 );
     Dialin( const Dialin &d );
     ~Dialin( );

     virtual void process( ModemSimV2 &m );

 private:
     int who;
     int time;
     int what;
 };

//xvxvxvxvxvxvxvxvxvxvxvxvxvxvxvxvxvxvxvxvxvxvxvxvxvxvxvxvxvxvxvxvx
//++++++++++++++++++++++++++ EventHeap ++++++++++++++++++++++++++++

class EventHeap{

public:
    EventHeap();
    EventHeap( int numIndex );

    bool empty( ) const;
    const int & findMin( ) const;

    void push( const Event x );
    Event * pop();

private:
    int size;         // Number of elements in heap
    vector <Event*> array;            // The heap array

    void buildHeap( int index );
    void reIndex( int hole );
    int getLeft( int index ) const;
    int getRight( int index )const;
    int getParent( int index )const;
};

//xvxvxvxvxvxvxvxvxvxvxvxvxvxvxvxvxvxvxvxvxvxvxvxvxvxvxvxvxvxvxvxvx
//++++++++++++++++++++++++++ ModemSimV2 +++++++++++++++++++++++++++

class ModemSimV2{

public:
    ModemSimV2( int modems, double avgLen, int callIntrvl, EventHeap e );
    // Add a call to eventSet at the current time,
    // and schedule one for delta in the future.
    void nextCall( int delta );

    // Run the simulation
    void runSim( int stoppingTime );// = INT_MAX );

    friend class Event;

private:
    Random r;                       // A random source
    EventHeap *eventSet;                    // Pending events

    // Basic parameters of the simulation
    int freeModems;                 // Number of modems unused
    const double avgCallLen;        // Length of a call
    const int freqOfCalls;          // Interval between calls
};


#endif
4

1 回答 1

2

问题是您将前向声明放在标题中的错误位置。该process方法认为ModemSimV2属于Event,因此Event::ModemSimV2在错误消息中。从受保护的部分移出class ModemSimV2;到班级上方。

class ModemSimV2;

class Event
{
    ...

Alsothis是一个指针ModemSimV2,您需要在将其传递给process.

e->process(*this);
于 2012-04-10T18:14:33.343 回答