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

またthis、へのポインタでModemSimV2あり、に渡す前に逆参照する必要がありますprocess

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