2012-04-10 4 views
0

私は多くの異なる前方宣言の組み合わせで遊んでいましたが、これは最善のものと思われました。C++フォワード宣言エラー - Lvalueをバインドすることはできません

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

を、私は本当にそれが何を意味するのか理解していない、任意の助けになります。私はラインに

e->process(this); 

をコメント解除と私が言うのXcodeからのエラーを取得するまでは、コンパイルされただけでした感謝する。

おかげで、

出典:

#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 

答えて

2

問題は、あなたがヘッダに間違った場所にあなたの前方宣言を入れています。 processメソッドは、ModemSimV2Eventに属していると考え、エラーメッセージにEvent::ModemSimV2とします。保護されたセクションからclass ModemSimV2;をクラスの上に移動します。

class ModemSimV2; 

class Event 
{ 
    ... 

またthisModemSimV2へのポインタで、あなたはprocessに渡す前に、それを間接参照する必要があります。

e->process(*this); 
+0

私はそれを試してみたが、私は同じエラー – dajee

+0

[OK]を、あなたは前方に、私は私の答えを更新します間違った場所に宣言を取得します。 – Joe

関連する問題