麻烦搞清楚这个代码在Linux上有什么问题

时间:2014-02-05 23:01:20

标签: c++ g++

我无法弄清楚为什么我的代码不能在linux上运行。我收到的shared_ptrs错误如:(部分列表)

roject.cpp:21: error: ISO C++ forbids declaration of 'shared_ptr' with no type
project.cpp:21: error: expected ';' before '<' token
project.cpp:22: error: ISO C++ forbids declaration of 'shared_ptr' with no type
project.cpp:22: error: expected ';' before '<' token
project.cpp:23: error: ISO C++ forbids declaration of 'shared_ptr' with no type
project.cpp:23: error: expected ';' before '<' token
project.cpp:30: error: ISO C++ forbids declaration of 'shared_ptr' with no type
project.cpp:30: error: expected ';' before '<' token
project.cpp:37: error: ISO C++ forbids declaration of 'shared_ptr' with no type
project.cpp:37: error: expected ';' before '<' token
project.cpp:82: error: 'shared_ptr' has not been declared
project.cpp:82: error: expected ',' or '...' before '<' token
project.cpp:153: error: 'shared_ptr' has not been declared
project.cpp:153: error: expected ',' or '...' before '<' token
project.cpp: In member function 'void device_table::addCore(int)':
project.cpp:86: error: 'process' was not declared in this scope
project.cpp:88: error: 'struct device_item' has no member named 'process'
project.cpp:89: error: 'currentTime' was not declared in this scope
project.cpp:92: error: 'struct device_item' has no member named 'process'
project.cpp:98: error: 'struct device_item' has no member named 'process'
project.cpp:104: error: 'struct device_item' has no member named 'process'
project.cpp:110: error: 'struct device_item' has no member named 'process'
project.cpp:124: error: 'process_state' is not a class or namespace
project.cpp:130: error: 'process_state' is not a class or namespace
project.cpp:137: error: 'struct device_item' has no member named 'process'
project.cpp:137: error: 'process' was not declared in this scope
project.cpp:138: error: 'currentTime' was not declared in this scope
project.cpp:140: error: 'process_state' is not a class or namespace
project.cpp: In member function 'void device_table::addDisk(int)':
project.cpp:156: error: 'struct device_item' has no member named 'process'
project.cpp:156: error: 'disk' was not declared in this scope
project.cpp:157: error: 'currentTime' was not declared in this scope
project.cpp:167: error: 'process_state' is not a class or namespace
project.cpp: In member function 'void device_table::checkCoreAndDisk(int)':

这是我的代码。我意识到我不应该使用矢量。下一次总会有。

#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <memory>
#include <algorithm>
#include <queue>

using namespace std;

enum process_state { P_NONE, READY, RUNNING, WAITING };
enum event_component { E_NONE, START, CORE, TTY, DISK };

struct data_entry {
    string text;
    int time;
};

struct process_entry {
    int process;
    shared_ptr<data_entry> * dataStart;
    shared_ptr<data_entry> * dataEnd;
    shared_ptr<data_entry> * dataCurrent;
    process_state state;
    int Deadline;
    int totalTime;
};

struct event_entry {
    shared_ptr<process_entry> * process;
    int time;
    event_component component;

};

struct device_item {
    shared_ptr<process_entry> * process;
    int finishTime;

    int startTime;
    int endTime;
};

class device_table {
    device_item * core1;
    device_item * core2;
    device_item * core3;
    device_item * core4;
    device_item * disk1;

    std::queue<device_item*> IQ;
    std::queue<device_item*> RQ;
    std::queue<device_item*> DiskQ;

    int completedReal;
    int completedInter;
    int completedDisk;

    double DurationDisk;
    int CoreUtilization[4];
    int DiskUtilization;

public:
    device_table()
    {
        core1 = NULL;
        core2 = NULL;
        core3 = NULL;
        core4 = NULL;
        disk1 = NULL;
        completedReal = 0;
        completedInter = 0;
        completedDisk = 0;
        DurationDisk = 0;
        DiskUtilization = 0;

        CoreUtilization[0] = 0;
        CoreUtilization[1] = 0;
        CoreUtilization[2] = 0;
        CoreUtilization[3] = 0;
    }
    void addCore(shared_ptr<process_entry> * process, int currentTime)
    {
        if((core1 != NULL && core2 != NULL && core3 != NULL && core4 != NULL))
        {
            string processType = process->get()->dataStart->get()->text;
            device_item * newEntry = new device_item();
            newEntry->process = process;
            newEntry->finishTime = currentTime + process->get()->dataCurrent->get()->time;
            newEntry->startTime = currentTime;

            if(core1->process->get()->dataStart->get()->text == "INTERACTIVE" && processType == "REAL-TIME")
            {
                core1->finishTime = core1->finishTime - currentTime;
                IQ.push(core1);
                core1 = newEntry;
            }
            else if(core2->process->get()->dataStart->get()->text == "INTERACTIVE" && processType == "REAL-TIME")
            {
                core2->finishTime = core2->finishTime - currentTime;
                IQ.push(core2);
                core2 = newEntry;
            }
            else if(core3->process->get()->dataStart->get()->text == "INTERACTIVE" && processType == "REAL-TIME")
            {
                core3->finishTime = core3->finishTime - currentTime;
                IQ.push(core3);
                core3 = newEntry;
            }
            else if(core4->process->get()->dataStart->get()->text == "INTERACTIVE" && processType == "REAL-TIME")
            {
                core4->finishTime = core4->finishTime - currentTime;
                IQ.push(core4);
                core4 = newEntry;
            }
            else
            {
                newEntry->finishTime = 0;
                newEntry->startTime = 0;

                if(process->get()->dataCurrent->get()->text == "INTERACTIVE")
                {
                    IQ.push(newEntry);
                    process->get()->state = process_state::WAITING;
                }
                else
                {
                    newEntry->finishTime = process->get()->dataCurrent->get()->time;
                    RQ.push(newEntry);
                    process->get()->state = process_state::WAITING;
                }
            }
        }
        else
        {
            device_item * newEntry = new device_item();
            newEntry->process = process;
            newEntry->startTime = currentTime;
            newEntry->finishTime = currentTime + process->get()->dataCurrent->get()->time;
            process->get()->state = process_state::RUNNING;

            if(core1 == NULL)
                core1 = newEntry;
            else if(core2 == NULL)
                core2 = newEntry;
            else if(core3 == NULL)
                core3 = newEntry;
            else if(core4 == NULL)
                core4 = newEntry;
        }

    }
    void addDisk(shared_ptr<process_entry> * disk, int currentTime)
    {
        device_item * newDisk = new device_item();
        newDisk->process = disk;
        newDisk->finishTime = currentTime + disk->get()->dataCurrent->get()->time;
        newDisk->startTime = currentTime;

        if(disk1 == NULL)
            disk1 = newDisk;
        else
        {
            newDisk->finishTime = 0;
            newDisk->startTime = 0;
            DiskQ.push(newDisk);
            disk->get()->state = process_state::WAITING;
        }
    }
    void checkCoreAndDisk(int currentTime)
    {
        // Stick queue if becomes empty. Subtract time.

        if(disk1 != NULL)
        {
            if(disk1->finishTime <= currentTime) {
                DiskUtilization += disk1->finishTime - disk1->startTime;
                disk1->process->get()->state = process_state::READY;
                DurationDisk += disk1->finishTime - disk1->startTime;
                disk1 = NULL;
                completedDisk += 1;

                if(DiskQ.size() > 0) {
                    disk1 = DiskQ.front();
                    disk1->startTime = currentTime;
                    disk1->finishTime = currentTime + disk1->process->get()->dataCurrent->get()->time;
                    DiskQ.pop();
                    disk1->process->get()->state = process_state::RUNNING;
                }
            }
        }
        if(core1 != NULL)
        {
            if(core1->finishTime <= currentTime) {
                CoreUtilization[0] += core1->finishTime - core1->startTime;
                core1->process->get()->state = process_state::READY;
                if(core1->process->get()->dataStart->get()->text == "INTERACTIVE") { completedInter += 1;  } else { completedReal += 1; };

                core1 = NULL;

                if(RQ.size() > 0) {
                    core1 = RQ.front();
                    core1->startTime = currentTime;
                    core1->finishTime = currentTime + core1->finishTime;
                    RQ.pop();
                    core1->process->get()->state = process_state::RUNNING;
                }
                else if(IQ.size() > 0) {
                    core1 = IQ.front();
                    core1->startTime = currentTime;
                    core1->finishTime = currentTime + core1->finishTime;
                    IQ.pop();
                    core1->process->get()->state = process_state::RUNNING;
                }
            }
        }
        if(core2 != NULL)
        {
            if(core2->finishTime <= currentTime) {
                CoreUtilization[1] += core2->finishTime - core2->startTime;
                core2->process->get()->state = process_state::READY;
                if(core2->process->get()->dataStart->get()->text == "INTERACTIVE") { completedInter += 1; } else { completedReal += 1; };
                core2 = NULL;

                if(RQ.size() > 0) {
                    core2 = RQ.front();
                    core2->startTime = currentTime;
                    core2->finishTime = currentTime + core2->finishTime;
                    RQ.pop();
                    core2->process->get()->state = process_state::RUNNING;
                }
                else if(IQ.size() > 0) {
                    core2 = IQ.front();
                    core2->startTime = currentTime;
                    core2->finishTime = currentTime + core2->finishTime;
                    IQ.pop();
                    core2->process->get()->state = process_state::RUNNING;
                }
            }
        }
        if(core3 != NULL)
        {
            if(core3->finishTime <= currentTime) {
                CoreUtilization[2] += core3->finishTime - core3->startTime;
                core3->process->get()->state = process_state::READY;
                if(core3->process->get()->dataStart->get()->text == "INTERACTIVE") { completedInter += 1; } else { completedReal += 1; };
                core3 = NULL;

                if(RQ.size() > 0) {
                    core3 = RQ.front();
                    core3->startTime = currentTime;
                    core3->finishTime = currentTime + core3->finishTime;
                    RQ.pop();
                    core3->process->get()->state = process_state::RUNNING;
                }
                else if(IQ.size() > 0) {
                    core3 = IQ.front();
                    core3->startTime = currentTime;
                    core3->finishTime = currentTime + core3->finishTime;
                    IQ.pop();
                    core3->process->get()->state = process_state::RUNNING;
                }
            }
        }
        if(core4 != NULL)
        {
            if(core4->finishTime <= currentTime) {
                CoreUtilization[3] += core4->finishTime - core4->startTime;
                core4->process->get()->state = process_state::READY;
                if(core4->process->get()->dataStart->get()->text == "INTERACTIVE") { completedInter += 1; } else { completedReal += 1; };
                core4 = NULL;

                if(RQ.size() > 0) {
                    core4 = RQ.front();
                    core4->startTime = currentTime;
                    core4->finishTime = currentTime + core4->finishTime;
                    RQ.pop();
                    core4->process->get()->state = process_state::RUNNING;
                }
                else if(IQ.size() > 0) {
                    core4 = IQ.front();
                    core4->startTime = currentTime;
                    core4->finishTime = currentTime + core4->finishTime;
                    IQ.pop();
                    core4->process->get()->state = process_state::RUNNING;
                }
            }
        }
    }

    bool checkDeadlines()
    {

    }

    bool checkCoreFull()
    {
        return (core1 != NULL && core2 != NULL && core3 != NULL && core4 != NULL);
    }

    int printCompletedInt()
    {
        return completedInter;
    }

    int printCompletedReal()
    {
        return completedReal;
    }

    int printCompletedDisk()
    {
        return completedDisk;
    }

    double printAvgDiskDuration()
    {
        return (DurationDisk / completedDisk);
    }

    int printCoreUtilization(int core)
    {
        return CoreUtilization[core - 1];
    }

    int printDiskUtilization()
    {
        return DiskUtilization;
    }
};

void createDataTable(string file, vector<shared_ptr<data_entry>>  &dataTable)
{
    string line;
    ifstream myfile (file);
    if (myfile.is_open())
    {
        int processAmount = 0;

        while ( getline (myfile,line) )
        {
            if(line.find("INTERACTIVE") != string::npos)
            {
                shared_ptr<data_entry> newEntry = make_shared<data_entry>();
                newEntry->text = "INTERACTIVE";
                newEntry->time = atoi(line.substr(12, 12).c_str());

                dataTable.push_back(newEntry);

                //shared_ptr<process_entry> newProcessEntry = shared_ptr<process_entry>(new process_entry);
                //newProcessEntry->dataStart = &dataTable.at(dataTable.size() - 1);
                //newProcessEntry->dataCurrent = &dataTable.at(dataTable.size() - 1);
                //newProcessEntry->process = processAmount;

                //processTable.push_back(newProcessEntry);

                processAmount += 1;
            }
            else if (line.find("REAL-TIME") != string::npos)
            {
                shared_ptr<data_entry> newEntry = make_shared<data_entry>();
                newEntry->text = "REAL-TIME";
                newEntry->time = atoi(line.substr(10, 10).c_str());
                dataTable.push_back(newEntry);
                processAmount += 1;
            }
            else if(line.find("END") != string::npos)
            {
                shared_ptr<data_entry> newEntry = make_shared<data_entry>();
                newEntry->text = "END";
                newEntry->time = -1;
                dataTable.push_back(newEntry);

                //processTable.at(processTable.size())->dataEnd = &dataTable.at(dataTable.size());
            }
            else if(line.find("RUN") != string::npos)
            {
                shared_ptr<data_entry> newEntry = make_shared<data_entry>();
                newEntry->text = "RUN";
                newEntry->time = atoi(line.substr(4, 4).c_str());
                dataTable.push_back(newEntry);
            }
            else if(line.find("TTY") != string::npos)
            {
                shared_ptr<data_entry> newEntry = make_shared<data_entry>();
                newEntry->text = "TTY";
                newEntry->time = atoi(line.substr(4, 4).c_str());
                dataTable.push_back(newEntry);
            }
            else if(line.find("DISK") != string::npos)
            {
                shared_ptr<data_entry> newEntry = make_shared<data_entry>();
                newEntry->text = "DISK";
                newEntry->time = atoi(line.substr(5, 5).c_str());
                dataTable.push_back(newEntry);
            }
            else if(line.find("DEADLINE") != string::npos)
            {
                shared_ptr<data_entry> newEntry = make_shared<data_entry>();
                newEntry->text = "DEADLINE";
                newEntry->time = atoi(line.substr(9, 9).c_str());
                dataTable.push_back(newEntry);
            }
        }
    }

    myfile.close();
}

void createProcessTable(vector<shared_ptr<data_entry>> &dataTable, vector<shared_ptr<process_entry>> &processTable)
{
    int processAmount = 0;

    for(size_t i = 0; i < dataTable.size(); i++)
    {
        if(dataTable.at(i)->text == "INTERACTIVE" || dataTable.at(i)->text == "REAL-TIME")
        {
            shared_ptr<process_entry> newProcessEntry = make_shared<process_entry>();
            newProcessEntry->dataStart = &dataTable.at(i);
            newProcessEntry->dataCurrent = &dataTable.at(i);
            newProcessEntry->process = processAmount;
            newProcessEntry->state = process_state::P_NONE;
            newProcessEntry->totalTime = 0;

            if(dataTable.at(i + 1)->text == "DEADLINE")
                newProcessEntry->Deadline = dataTable.at(i + 1)->time;
            else
                newProcessEntry->Deadline = -1;

            processTable.push_back(newProcessEntry);

            processAmount += 1;
        }
        else if(dataTable.at(i)->text == "END")
        {
            processTable.back()->dataEnd = &dataTable.at(i);
        }

        if(processTable.size() > 0 && dataTable.at(i)->text != "DEADLINE" && dataTable.at(i)->time != -1)
        {
            processTable.back()->totalTime += dataTable.at(i)->time;
        }
    }
}

void createEventTable(vector<shared_ptr<process_entry>> &processTable, vector<shared_ptr<event_entry>> &eventTable)
{
    for(size_t i = 0; i < processTable.size(); i++)
    {
        shared_ptr<event_entry> newEventEntry = make_shared<event_entry>();
        newEventEntry->process = &processTable.at(i);
        newEventEntry->time = newEventEntry->process->get()->dataStart->get()->time;
        newEventEntry->component = event_component::E_NONE;
        eventTable.push_back(newEventEntry);
    }
}

bool sortEventTable(shared_ptr<event_entry> i, shared_ptr<event_entry> j)
{
    return (i->time < j->time);
}

int main(int argc, const char *argv[])
{
    int currentTime = 0;
    int deadlineAmount = 0;

    vector<shared_ptr<data_entry>>  dataTable;
    vector<shared_ptr<process_entry>> processTable;
    vector<shared_ptr<event_entry>> eventTable;
    device_table newDevice;

    createDataTable(argv[1], dataTable);
    createProcessTable(dataTable, processTable);
    createEventTable(processTable, eventTable);
    std::sort (eventTable.begin(), eventTable.end(), sortEventTable);

    while(eventTable.size() > 0)
    {
        std::sort (eventTable.begin(), eventTable.end(), sortEventTable);

        newDevice.checkCoreAndDisk(currentTime);
        //newDevice.printAll();
        string currentText = eventTable.at(0).get()->process->get()->dataCurrent->get()->text;
        if(currentText == "RUN")
        {
            eventTable.at(0).get()->component = event_component::CORE;
            newDevice.addCore(eventTable.at(0).get()->process, currentTime);
        }
        else if(currentText == "REAL-TIME" || currentText == "INTERACTIVE")
        {

            eventTable.at(0).get()->component = event_component::START;
        }
        else if(currentText == "DISK")
        {
            eventTable.at(0).get()->component = event_component::DISK;
            newDevice.addDisk(eventTable.at(0).get()->process, currentTime);
        }
        else if(currentText == "TTY")
        {
            eventTable.at(0).get()->component = event_component::TTY;
        }

        if(eventTable.at(0).get()->process->get()->dataCurrent->get()->text == "END")
        {
            //cout << eventTable.at(0).get()->time << "\n";
            if(eventTable.at(0).get()->process->get()->Deadline == 1)
            {

            }
            eventTable.erase(eventTable.begin());
        }
        else
        {
            //cout << eventTable.at(0).get()->time << "\n";
            eventTable.at(0).get()->process->get()->dataCurrent += 1;

            if(currentText != "DEADLINE")
                currentTime = eventTable.at(0).get()->time;

            if(eventTable.at(0).get()->process->get()->dataCurrent->get()->time != -1 && currentText != "DEADLINE")
                eventTable.at(0).get()->time = currentTime + eventTable.at(0).get()->process->get()->dataCurrent->get()->time;
        }

        //cout << newDevice.checkCoreFull() << "\n";
    }

    for(size_t i = 0; i < processTable.size(); i++)
    {
        if(processTable.at(i)->Deadline != -1)
        {
            if (processTable.at(i)->totalTime > processTable.at(i)->Deadline)
                deadlineAmount += 1;
        }
    }

    cout << "Completed Interactive : " << newDevice.printCompletedInt() << "\n";
    cout << "Completed Real-Time   : " << newDevice.printCompletedReal() << "\n";
    cout << "Completed Disk        : " << newDevice.printCompletedDisk() << "\n";
    cout << "Core1 Utilization     : " << newDevice.printCoreUtilization(1) << "\n";
    cout << "Core2 Utilization     : " << newDevice.printCoreUtilization(2) << "\n";
    cout << "Core3 Utilization     : " << newDevice.printCoreUtilization(3) << "\n";
    cout << "Core4 Utilization     : " << newDevice.printCoreUtilization(4) << "\n";
    cout << "Disk Utilization      : " << newDevice.printDiskUtilization() << "\n";
    cout << "Average Disk Duration : " << newDevice.printAvgDiskDuration() << "\n";
    cout << "% Missed Deadline     : " << ((float)deadlineAmount / processTable.size()) * 100 << "%\n";
    cout << "Total Elapsed Time    : " << currentTime << "\n";

    int input;
    cin >> input;
    return 0;

}

1 个答案:

答案 0 :(得分:0)

您可能希望使用C ++ 11支持编译代码:

g++ -std=c++11