gpt4 book ai didi

C++ 前向声明错误 - 无法绑定(bind)值

转载 作者:行者123 更新时间:2023-11-28 03:38:32 25 4
gpt4 key购买 nike

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

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

最佳答案

问题是您将前向声明放在标题中的错误位置。 process 方法认为 ModemSimV2 属于 Event,因此出现错误消息中的 Event::ModemSimV2。将 class ModemSimV2; 移出 protected 部分,移至类之上。

class ModemSimV2;

class Event
{
...

此外,this 是指向 ModemSimV2 的指针,您需要在将其传递给 process 之前取消引用它。

e->process(*this);

关于C++ 前向声明错误 - 无法绑定(bind)值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10094076/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com