Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/148.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++编译错误-未知类型名称_C++_Data Structures_Compiler Errors - Fatal编程技术网

C++编译错误-未知类型名称

C++编译错误-未知类型名称,c++,data-structures,compiler-errors,C++,Data Structures,Compiler Errors,对于我的CS任务,我们要实现一个二进制堆来替换她的工作程序上的STL优先级队列,这是第一部分。对于第二部分,我们必须使用多态性重新实现它。我完成了第一部分,我所做的只是将所有类划分到它自己的头文件和源文件中,我得到了大量的错误。在xcode上显示未知类型名称“Event” 我试着换了ifndef的东西抱歉我不知道它叫什么,但是运气不好 任何帮助都将不胜感激,谢谢 大卫 我使用的是xcode,但以下是来自终端的错误消息: In file included from ModemSimV2.h:5,

对于我的CS任务,我们要实现一个二进制堆来替换她的工作程序上的STL优先级队列,这是第一部分。对于第二部分,我们必须使用多态性重新实现它。我完成了第一部分,我所做的只是将所有类划分到它自己的头文件和源文件中,我得到了大量的错误。在xcode上显示未知类型名称“Event”

我试着换了ifndef的东西抱歉我不知道它叫什么,但是运气不好

任何帮助都将不胜感激,谢谢

大卫

我使用的是xcode,但以下是来自终端的错误消息:

In file included from ModemSimV2.h:5,
                 from Event.h:4,
                 from Event.cpp:1:
EventHeap.h:18: error: expected ‘,’ or ‘...’ before ‘&’ token
EventHeap.h:18: error: ISO C++ forbids declaration of ‘Event’ with no type
EventHeap.h:19: error: ISO C++ forbids declaration of ‘Event’ with no type
EventHeap.h:19: error: expected ‘;’ before ‘*’ token
EventHeap.h:23: error: ISO C++ forbids declaration of ‘Event’ with no type
EventHeap.h:23: error: expected ‘;’ before ‘*’ token
In file included from Event.h:4,
                 from EventHeap.h:8,
                 from EventHeap.cpp:1:
ModemSimV2.h:11: error: ‘EventHeap’ has not been declared
ModemSimV2.h:23: error: ISO C++ forbids declaration of ‘EventHeap’ with no type
ModemSimV2.h:23: error: expected ‘;’ before ‘*’ token
EventHeap.cpp: In constructor ‘EventHeap::EventHeap()’:
EventHeap.cpp:6: error: call of overloaded ‘Event()’ is ambiguous
Event.h:10: note: candidates are: Event::Event(int, int)
Event.h:9: note:                 Event::Event()
EventHeap.cpp: In constructor ‘EventHeap::EventHeap(int)’:
EventHeap.cpp:12: error: call of overloaded ‘Event()’ is ambiguous
Event.h:10: note: candidates are: Event::Event(int, int)
Event.h:9: note:                 Event::Event()
EventHeap.cpp: In member function ‘void EventHeap::buildHeap(int)’:
EventHeap.cpp:40: error: call of overloaded ‘Event()’ is ambiguous
Event.h:10: note: candidates are: Event::Event(int, int)
Event.h:9: note:                 Event::Event()
main.cpp:

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


using namespace std;

// Simple main to test ModemSim class.
int main( )
{
    int numModems;
    int totalTime;
    double avgConnectTime;
    int dialInFrequency;

    cout << "Enter number of modems, length of simulation,\n"
    << " average connect time, how often calls occur: ";

    cin >> numModems >> totalTime >>
    avgConnectTime >> dialInFrequency;

    EventHeap eHeap( numModems );
    ModemSimV2 s( numModems, avgConnectTime, dialInFrequency, eHeap );
    s.runSim( totalTime );

    return 0;
}
ModemSimV2.cpp

#include "ModemSimV2.h"

// 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;

    eventSet->push( Event( userNum++, nextCallTime ) );
    nextCallTime += delta;
}

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

    while( !eventSet->empty( ) ){
        e = eventSet->pop();
        if ( e->time > stoppingTime )
            break;
        //e->process( this );
        nextCall( freqOfCalls );
    }
}
Event.h包括ModemSimV2.h。ModemSimV2.h包括Event.h。没有一点帮助,这是行不通的


你应该仔细阅读。有关更多信息,请参阅。

首先,不要让两个构造函数执行相同的操作。不能将事件和事件变量设置为0。它们都做同样的事情,如果你没有价值,就没有办法决定选择哪一个。这解决了问题,但我这么做只是因为Event需要了解ModemSim,ModemSim需要了解EventHeap,EventHeap需要了解Event。。。这是一个恶性循环。我该如何解决这个问题?@davidevent不需要ModemSimV2的定义。它只将其用作友元类,因此转发声明就足够了。阅读我答案中的链接。是的,但是仍然必须先声明一个类,然后再声明另一个类,即使您使用前向声明将所有类放在同一个文件中。不管上面是什么,都不知道下面是什么。我想这是我在实现中需要改进的地方?没关系,我想我已经做到了。我不知道还有第二个链接。非常感谢。
#include "Event.h"

Event::Event( ) {

}

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

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; 
}
#ifndef BINARY_HEAP_P2_H
#define BINARY_HEAP_P2_H

#include <iostream>
#include <cmath>
#include <vector>

#include "Event.h"

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
    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;
};


#endif
#include "EventHeap.h"

//Constructor
EventHeap::EventHeap( ) {

    array = new Event[1];
    size = 0;
}

EventHeap::EventHeap( int numVals ) { 

    array = new Event[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;
}
#ifndef MODEM_SIM_V2_H
#define MODEM_SIM_V2_H

#include "Event.h"
#include "EventHeap.h"
#include "random.h"


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
#include "ModemSimV2.h"

// 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;

    eventSet->push( Event( userNum++, nextCallTime ) );
    nextCallTime += delta;
}

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

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