"No appropriate default constructor" 我到处寻找答案

"No appropriate default constructor" I have looked everywhere for an answer

本文关键字:寻找 答案 constructor No appropriate default      更新时间:2023-10-16

我已经查看了有关此主题的所有帖子,但没有一个有帮助。请帮助我的作业在两个小时后到期。

这是相关的标题

    class RCB {
private:
    str                 rID;
    int                 numResources;
    int                 availableResources;
    int                 resourcesRequested;     //The amount of resources requested including resources requested from PCB's in the waitlist

public:
    std::list<PCB*>     waitList;
    RCB(str name, int r);
    ~RCB();
    void setRID(str name) { rID = name; }
    void allocateResources(int n) { availableResources -= n; }
    void returnResources(int n) { availableResources += n; }
    int getNumResources(){ return numResources; }
    int getAvailableResources(){ return availableResources; }
    str getRID(){ return rID; }
    int getResourcesRequested() { return resourcesRequested; }
};

这是源文件

  RCB::RCB(str name, int r) :
        rID{ name },
        numResources{ r },
        availableResources{ r },
        resourcesRequested{ 0 }
    {}

我收到错误

错误

1 错误 C2512:"RCB":没有可用的适当默认构造函数 C:\程序文件 (x86)\Microsoft Visual Studio 12.0\VC\include\tuple 746 1 进程调度程序

这是源的其余部分

#include "Data.h"

PCB::PCB(str name, int p) : 
    pID{ name },
    status{ READY },
    backptr{ nullptr },
    parent{ nullptr },
    priority{ p }
{
    //Insert resource counters into the resource log
    resourceLog.insert(std::pair<str, int>("R1", 0));
    resourceLog.insert(std::pair<str, int>("R2", 0));
    resourceLog.insert(std::pair<str, int>("R3", 0));
    resourceLog.insert(std::pair<str, int>("R4", 0));

    //Insert resource request counters into the request log
    requestBackLog.insert(std::pair<str, int>("R1", 0));
    requestBackLog.insert(std::pair<str, int>("R2", 0));
    requestBackLog.insert(std::pair<str, int>("R3", 0));
    requestBackLog.insert(std::pair<str, int>("R4", 0));
}

PCB::~PCB()
{


}
//Print the proccess ID to stdout
void PCB::printID()
{
    std::cout << pID << " ";
}




RCB::RCB(str name, int r) :
    rID{ name },
    numResources{ r },
    availableResources{ r },
    resourcesRequested{ 0 }
{}

RCB::RCB(const RCB& r)
{
    rID = r.rID;
    numResources = r.numResources;
    availableResources = r.availableResources;
    resourcesRequested = r.resourcesRequested;
}
RCB::RCB() :
    rID{ "R0" },
    numResources{ 5 },
    availableResources{ 5 },
    resourcesRequested{ 0 }
{
}

MainProcess::MainProcess() :
    initProcess("init", 0),
    currentlyRunning{ &initProcess }
{
    //Add init to the list of process names
    nameList.push_back("init");
    RCB R1("R1", 1);
    RCB R2("R2", 2);
    RCB R3("R3", 3);
    RCB R4("R4", 4);

    //Initialize four resources in the resource map
    resourceMap.insert(std::pair<str, RCB>("R1", R1));
    resourceMap.insert(std::pair<str, RCB>("R2", R2));
    resourceMap.insert(std::pair<str, RCB>("R3", R3));
    resourceMap.insert(std::pair<str, RCB>("R4", R4));

    //Add a reference to the initial process to lowest list on the ready list 
    readyList[0].push_back(&initProcess);
    initProcess.setStatus(RUNNING);
    //Print the id of the init process to stdout
    initProcess.printID();



}
void MainProcess::Release(str rID, int num)
{
    //Check for negative numbers
    if (num < 0){
        std::cout << "error ";
        return;
    }

    //Subtract resources from the resource log of the currently running process
    currentlyRunning->resourceLog[rID] = std::max(currentlyRunning->resourceLog[rID] - num, 0);
    //Add resources back to the available resources
    resourceMap[rID].returnResources(num);
    //Iterator pointing to the first PCB in the waitlist
    std::list<PCB*>::iterator it = resourceMap[rID].waitList.begin();
    //CONDITION
    //The waitlist on the RCB is not empty AND 
    //the amount of available resources in the RCB is greater than the amount of
    //requested resources by the PCB at the front of the waitlist
    while (!resourceMap[rID].waitList.empty() && resourceMap[rID].getAvailableResources() > 0 && it != resourceMap[rID].waitList.end())
    {
        if ((*it)->requestBackLog[rID] <= resourceMap[rID].getAvailableResources() ){
            //Allocate backloged resources from the RCB
            resourceMap[rID].allocateResources((*it)->requestBackLog[rID]);
            //Give the allocated resources to the PCB
            (*it)->resourceLog[rID] += (*it)->requestBackLog[rID];
            //Zero out the request back log
            (*it)->requestBackLog[rID] = 0;

            //Change the status of the PCB and add it to the ready list         
            (*it)->setStatus(READY);
            (*it)->setBackPtr(readyList);
            readyList[(*it)->getPriority()].push_back((*it));
            //Move the iterator to the next element in the wait list
            it++;
            //Remove the PCB from the waitlist
            resourceMap[rID].waitList.pop_front();

            //Call the scheduler
            Scheduler();

        }

        //If the PCB at the front of the wait list is asking for too many resources
        //then just skip it and move on to the next PCB on the waitlist
        else
        {
            it++;
        }

    }

}
void MainProcess::FreeResources(PCB* p)
{
    //Free all the resources currently held by PCB p
    for (auto& item : p->resourceLog){
        Release(item.first, item.second);
    }

}
void MainProcess::Destroy(str PID)
{
    //Search the ready list for the PCB with the specified PID
    for (int i = 1; i < 3; i++){
        for (PCB* p : readyList[i]){
            if (p->getPID() == PID){
                KillTree(p);
                Scheduler();
                return;
            }
        }
    }
    //If no PCB was found with PID then output error
    std::cout << "error ";

}
void MainProcess::KillTree(PCB* p)
{
    //Recursively call kill tree on each child
    if (!p->children.empty()){
        for (PCB* child : p->children)
        {
            KillTree(child);
        }
    }
    //If the PCB has no children then free all of its resources and delete it.
    FreeResources(p);
    delete p;
    return;

}
void MainProcess::Create(str name, int p)
{
    std::vector<str>::iterator it = std::find(nameList.begin(), nameList.end(), name);
    if (it != nameList.end()){
        std::cout << "error ";
        return;
    }
    else{
        //Add the new process name to the list of process names
        nameList.push_back(name);
        //Create a reference to a new process
        PCB* newProcess = new PCB(name, p);
        //Set the back pointer to the ready list
        newProcess->setBackPtr(readyList);
        //Set the parent pointer to the parent process
        newProcess->setParent(currentlyRunning);
        //Add it to the process tree
        currentlyRunning->children.push_back(newProcess);
        //Add the new process to the ready list
        newProcess->setStatus(READY);
        readyList[newProcess->getPriority()].push_back(newProcess);
        //Call the scheduler
        Scheduler();
    }

}

void MainProcess::Request(str rID, int num)
{
    RCB* rcb;
    //Check for negative numbers
    if (num < 0){
        std::cout << "error ";
        return;
    }
    //rcb is a reference to a resource in the resource map
    rcb = &resourceMap[rID];
    //If resources are available
    if (rcb->getAvailableResources() >= num){
        rcb->allocateResources(num);
        //Give the requested resources to the running process
        currentlyRunning->resourceLog[rID] += num;


        //Insert RCB into resource list of currently running process if not in there already
        std::list<RCB*>::iterator it = std::find(currentlyRunning->other_Resources.begin(), currentlyRunning->other_Resources.end(), rcb);
        if (it == currentlyRunning->other_Resources.end()){
            currentlyRunning->other_Resources.push_back(rcb);
        }
    }

    else
    {
        //Stop the process from running and put it on the blocked list
        currentlyRunning->setStatus(BLOCKED);
        currentlyRunning->setBackPtr(&rcb->waitList);
        readyList[currentlyRunning->getPriority()].pop_front();
        rcb->waitList.push_back(currentlyRunning);
        //Log the requested amount in the request back log
        currentlyRunning->requestBackLog[rID] += num;
        //Call Scheduler
        Scheduler();

    }
}

void MainProcess::Scheduler()
{
    PCB* p = getHighestPriority();
    if (currentlyRunning != p){
        //Set the currently running process to the 
        currentlyRunning = p;
        //Set the status of the currently running process to "RUNNING"
        currentlyRunning->setStatus(RUNNING);
    }
    //Print the id of the currently running process
    currentlyRunning->printID();
}
void MainProcess::TimeOut()
{

    //Remove currently running process from the front of the list and put it in the back
    readyList[currentlyRunning->getPriority()].pop_front();
    currentlyRunning->setStatus(READY);
    readyList[currentlyRunning->getPriority()].push_back(currentlyRunning);
    Scheduler();
}
PCB* MainProcess::getHighestPriority()
{
    //Iterate through the ready list to find the highest priority process
    for (int i = 2; i >= 0; i--){
        if (!readyList[i].empty()){
            return readyList[i].front();
        }
    }
    //Return null if no process is found
    return nullptr;
}
void MainProcess::Reset()
{
    for (PCB* p : initProcess.children){
        KillTree(p);
    }
    Scheduler();

}

我很确定这就是问题所在

//Add init to the list of process names
    nameList.push_back("init");
    RCB R1("R1", 1);
    RCB R2("R2", 2);
    RCB R3("R3", 3);
    RCB R4("R4", 4);

    //Initialize four resources in the resource map
    resourceMap.insert(std::pair<str, RCB>("R1", R1));
    resourceMap.insert(std::pair<str, RCB>("R2", R2));
    resourceMap.insert(std::pair<str, RCB>("R3", R3));
    resourceMap.insert(std::pair<str, RCB>("R4", R4));

当您定义参数化构造函数 [RCB(str name, int r)] 时,编译器不会生成默认的无参数构造函数。因此,在您的情况下,如果您尝试创建 RCB [RCB obj;] 的对象,它势必会抛出此错误。要解决,您需要定义一个默认的无参数构造函数。不确定这是否有帮助。

根据错误,类 RCB 没有默认构造函数。这很好。并非所有类都必须具有默认构造函数。但是,代码中的某些内容使用 RCB(不是问题中包含的内容的一部分)正在尝试显式或隐式调用 RCB 的默认构造函数。

例如,如果您在代码中的某个位置声明

RCB rcb;

然后,您将使用默认构造函数。